百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

centos7稳定nginx搭建,快速搭建nginx

suiw9 2024-11-14 19:11 25 浏览 0 评论

程序人生地址:

https://www.hryc.xyz/archives/centos7--xia-da-jian--n-g-i-n-x

一、安装准备


首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 所以执行如下命令安装

$ yum install gcc-c++

$ yum install pcre pcre-devel

$ yum install zlib zlib-devel

$ yum install openssl openssl-devel


二、安装Nginx


安装之前,最好检查一下是否已经安装有nginx

$ find -name nginx


如果系统已经安装了nginx,那么就先卸载

$ yum remove nginx


首先进入/data目录

$ cd /data


从官网下载最新版的nginx

$ wget http://nginx.org/download/nginx-1.11.10.tar.gz


解压nginx压缩包

$ tar -zxvf nginx-1.11.10.tar.gz


删除下载的nginx

$ rm -rf nginx-1.11.10.tar.gz


会产生一个nginx-1.11.10 目录,这时进入nginx-1.11.10目录

$ mv nginx-1.11.10 nginx

$ cd nginx


接下来安装,使用--prefix参数指定nginx安装的目录,make、make install安装

./configure --prefix=/data/nginx --conf-path=/data/nginx/nginx.conf

1.$ ./configure --prefix=/data/nginx --conf-path=/data/nginx/nginx.conf

$默认安装在/usr/local/nginx

2.$ make

3.$ make install


如果没有报错,顺利完成后,最好看一下nginx的安装目录

$ whereis nginx

nginx安装在了/data/nginx目录下


默认的安装路径为:/data/nginx;跳转到其目录下sbin路径下,便可以启动或停止它了。

启动:/data/nginx/sbin/nginx

关闭:/data/nginx/sbin/nginx -s stop

重启: /data/nginx/sbin/nginx -c /home/zhyg/nginx/nginx.conf


防火墙开放80端口

增加80端口到防火墙配置中,执行以下操作:

[root@localhost ~]/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #开启80端口

[root@localhost ~]/etc/rc.d/init.d/iptables save #保存配置 service iptables save

[root@localhost ~]/etc/rc.d/init.d/iptables restart #重启防火墙 service iptables restart


3、添加到系统服务

使用命令“# vi /etc/init.d/nginx”,打开编辑器,输入如下内容:

#!/bin/sh 
# chkconfig: 2345 85 15 
# Startup script for the nginx Web Server 
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
# description: nginx is a World Wide Web server. 
# It is used to serve HTML files and CGI. 
# processname: nginx 
# pidfile: /data/nginx/logs/nginx.pid 
# config: /data/nginx/conf/nginx.conf 
  
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 
DESC="nginx deamon" 
NAME=nginx 
DAEMON=/data/nginx/sbin/$NAME 
SCRIPTNAME=/etc/init.d/$NAME
  
test -x $DAEMON || exit 0
  
d_start(){
  $DAEMON || echo -n "already running"  
}
  
d_stop(){
  $DAEMON -s quit || echo -n "not running" 
}
  
  
d_reload(){
  $DAEMON -s reload || echo -n "can not reload"  
}
  
case "$1" in
start)
  echo -n "Starting Ngnix"
  d_start
  echo "." 
;;  
stop)
  echo -n "Stopping Ngnix"
  d_stop
  echo "."
;;
reload)
  echo -n "Reloading $DESC conf..."
  d_reload
  echo "reload ."
;;
restart)
  echo -n "Restarting Ngnix"
  d_stop
  sleep 2
  d_start
  echo "."
;;
*)
  echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2
  exit 3
;;
esac
  
exit 0


保存退出后,再使用下面的命令,使其可执行;然后,添加配置并查看。

可用chkconfig修改其值,也可用ntsysv工具改变是否自启动。

# chmod +x /etc/init.d/nginx

# chkconfig --add nginx

# chkconfig nginx on/off

# chkconfig --list nginx

nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off


启动 service nginx start

停止 service nginx stop

重启 service nginx restart


安装完毕后,进入安装后目录(/data /nginx)便可以启动或停止它了。

到此,使用CentOS安装nginx已经完成了。

nginx 配置路径重定向地址:

location / {

rewrite ^(.*) http://10.204.20.128/front/index.html#/login break;

}


新版配置:


#!/bin/bash


# nginx Startup script for the Nginx HTTP Server


# it is v.0.0.2 version.


# chkconfig: - 85 15


# description: Nginx is a high-performance web and proxy server.


# It has a lot of features, but it's not for everyone.


# processname: nginx


# pidfile: /var/run/nginx.pid


# config: /usr/local/nginx/conf/nginx.conf


nginxd=/usr/local/nginx/sbin/nginx


nginx_config=/usr/local/nginx/conf/nginx.conf


nginx_pid=/var/run/nginx.pid


RETVAL=0


prog="nginx"


# Source function library.


. /etc/rc.d/init.d/functions


# Source networking configuration.


. /etc/sysconfig/network


# Check that networking is up.


[ ${NETWORKING} = "no" ] && exit 0


[ -x $nginxd ] || exit 0


# Start nginx daemons functions.


start() {


if [ -e $nginx_pid ];then


echo "nginx already running...."


exit 1


fi


echo -n #34;Starting $prog: "


daemon $nginxd -c ${nginx_config}


RETVAL=$?


echo


[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx


return $RETVAL


}


# Stop nginx daemons functions.


stop() {


echo -n #34;Stopping $prog: "


killproc $nginxd


RETVAL=$?


echo


[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid


}


# reload nginx service functions.


reload() {


echo -n #34;Reloading $prog: "


#kill -HUP `cat ${nginx_pid}`


killproc $nginxd -HUP


RETVAL=$?


echo


}


# See how we were called.


case "$1" in


start)


start


;;


stop)


stop


;;


reload)


reload


;;


restart)


stop


start


;;


status)


status $prog


RETVAL=$?


;;


*)


echo #34;Usage: $prog {start|stop|restart|reload|status|help}"


exit 1


esac


exit $RETVAL


把这个文档放到/etc/init.d/


然后执行


chkconfig --add /etc/init.d/nginx

chmod 755 /etc/init.d/nginx

chkconfig --add nginx


启动:/data/nginx/sbin/nginx

关闭:/data/nginx/sbin/nginx -s stop


查看进程:ps -A | grep nginx

原文地址:https://www.hryc.xyz/archives/centos7--xia-da-jian--n-g-i-n-x

相关推荐

5款Syslog集中系统日志常用工具对比推荐

一、为何要集中管理Syslog?Syslog由Linux/Unix系统及其他网络设备生成,广泛分布于整个网络。因其包含关键信息,可用于识别网络中的恶意活动,所以必须对其进行持续监控。将Sys...

跨平台、多数据库支持的开源数据库管理工具——DBeaver

简介今天给大家推荐一个开源的数据库管理工具——DBeaver。它支持多种数据库系统,包括Mysql、Oracle、PostgreSQL、SLQLite、SQLServer等。DBeaver的界面友好...

强烈推荐!数据库管理工具:Navicat Premium 16.3.2 (64位)

NavicatPremium,一款集数据迁移、数据库管理、SQL/查询编辑、智能设计、高效协作于一体的全能数据库开发工具。无论你是MySQL、MariaDB、MongoDB、SQLServer、O...

3 年 Java 程序员还玩不转 MongoDB,网友:失望

一、什么场景使用MongoDB?...

拯救MongoDB管理员的GUI工具大赏:从菜鸟到极客的生存指南

作为一名在NoSQL丛林中披荆斩棘的数据猎人,没有比GUI工具更称手的瑞士军刀了。本文将带你围观五款主流MongoDB管理神器的特性与暗坑,附赠精准到扎心的吐槽指南一、MongoDBCompass:...

mongodb/redis/neo4j 如何自己打造一个 web 数据库可视化客户端?

前言最近在做neo4j相关的同步处理,因为产线的可视化工具短暂不可用,发现写起来各种脚本非常麻烦。...

solidworks使用心得,纯干货!建议大家收藏

SolidWorks常见问题...

统一规约-关乎数字化的真正实现(规范统一性)

尽管数字化转型的浪潮如此深入人心,但是,对于OPCUA和TSN的了解却又甚少,这难免让人质疑其可实现性,因为,如果缺乏统一的语义互操作规范,以及更为具有广泛适用的网络与通信,则数字化实际上几乎难以具...

Elasticsearch节点角色配置详解(Node)

本篇文章将介绍如下内容:节点角色简介...

产前母婴用品分享 篇一:我的母婴购物清单及单品推荐

作者:DaisyH8746在张大妈上已经混迹很久了,有事没事看看“什么值得买”已渐渐成了一种生活习惯,然而却从来没有想过自己要写篇文章发布上来,直到由于我产前功课做得“太过认真”(认真到都有点过了,...

比任何人都光彩照人的假期!水润、紧致的肌肤护理程序

图片来源:谜尚愉快的假期临近了。身心振奋的休假季节。但是不能因为这种心情而失去珍贵的东西,那就是皮肤健康。炙热的阳光和强烈的紫外线是使我们皮肤老化的主犯。因此,如果怀着快乐的心情对皮肤置之不理,就会使...

Arm发布Armv9边缘AI计算平台,支持运行超10亿参数端侧AI模型

中关村在线2月27日消息,Arm正式发布Armv9边缘人工智能(AI)计算平台。据悉,该平台以全新的ArmCortex-A320CPU和领先的边缘AI加速器ArmEthos-U85NPU为核心...

柔性——面向大规模定制生产的数字化实现的基本特征

大规模定制生产模式的核心是柔性,尤其是体现在其对定制的要求方面。既然是定制,并且是大规模的定制,对于制造系统的柔性以及借助于数字化手段实现的柔性,就提出了更高的要求。面向大规模定制生产的数字化业务管控...

创建PLC内部标准——企业前进的道路

作者:FrankBurger...

标准化编程之 ----------- 西门子LPMLV30测试总结

PackML乃是由OMAC开发且被ISA所采用的自动化标准TR88.00.02,能够更为便捷地传输与检索一致的机器数据。PackML的主要宗旨在于于整个工厂车间倡导通用的“外观和感觉”,...

取消回复欢迎 发表评论: