一、安装Nginx所需要的依赖包
1 安装ssl功能需要的openssl库
1)下载地址 http://www.openssl.org/
2)安装命令
①解压文件: tar -zxvf openssl-1.1.1.tar.gz
② cd openssl-1.1.1
③ ./config
④ make
⑤ make install
2 安装gzip模块需要zlib库
1)下载地址 http://www.zlib.net/
2)安装命令
① 解压文件: tar -zxvf zlib-1.2.11.tar.gz
② cd zlib-1.2.11
③ ./configure
④ make
⑤ make install
3 安装 rewrite模块需要pcre库
1)下载地址 http://www.pcre.org/
2)安装命令
① 解压文件: tar -zxvf pcre-8.42.tar.gz
② cd pcre-8.42
③ ./configure
④ make
⑤ make install
二、安装Nginx
1、下载包
wget:http://101.96.8.164/nginx.org/download/nginx-1.10.1.tar.gz
2、解压缩包: tar -zxvf nginx-1.10.1.tar.gz
3、./configure --with-pcre=../pcre-8.42/ --with-zlib=../zlib-1.2.11/ --with-openssl=../openssl-1.1.1/
4、make
5、make install
三、启动Nginx
1、启动Nginx
[root@admin ~]# cd /usr/local/nginx
[root@admin sbin]# ./nginx
2、查看进程
nginx中采用工作进程来监听80端口,执行web操作,而主进程(第一行)来管理工作进程,一旦工作进程(第二行)挂了,主进程会主动启动一个工作进程,保证服务稳定。
3、查看80端口是否被监听:netstat -tulnp | grep :80
在页面用ip:80,若出现这个界面,则表示成功
若没有出现,则去配置相应的端口号
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
启动防火墙:
service iptables restart
重启Ng的命令
./nginx -s reload
四、设置service 便捷启动,关闭,重启nginx
1、在根目录下
2、进入etc —>init.d
创建 nginx脚本
touch nginx ---->vi nginx 填写下列脚本
#!/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=/usr/local/nginx/logs/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 $"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 $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
#reload nginx service functions.
reload() {
echo -n $"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 $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
3、添加执行权限
chmod u+x /etc/init.d/nginx
4、添加为系统服务
chkconfig --add nginx
5、开机自启动
chkconfig nginx on
6、启动nginx
service nginx start
7、关闭nginx
service nginx stop