JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

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

wys521 2024-09-27 13:50:53 精选教程 15 ℃ 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

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表