软件:nginx-1.10.3
任务:安装 Nginx
实战技巧:设置nginx自动启动服务
1.获取Nginx软件
[root@sky9890 tools]#
wget http://nginx.org/download/nginx-1.10.3.tar.gz
[root@sky9890 tools]# tar -zxvf nginx-1.10.3.tar.gz
[root@sky9890 tools]# cd nginx-1.10.3
[root@sky9890 nginx-1.10.3]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
2.编译安装Nginx
安装依赖包:
[root@sky9890 ~]# yum install pcre-devel zlib-devel openssl-devel
[root@sky9890 nginx-1.10.3]#./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module
[root@sky9890 nginx-1.10.3]#make && make install
3.Nginx的启动与停止
Nginx启动
[root@sky9890 /]# cd /usr/local/nginx/sbin/
[root@sky9890 sbin]# ./nginx
[root@sky9890 ~]# ps aux|grep ninx
root 19599 0.0 0.0 103336 900 pts/0 S+ 08:38 0:00 grep ninx
立即停止服务:
[root@sky9890 sbin]# ./nginx -s stop
从容停止服务:
[root@sky9890 sbin]# ./nginx -s quit
通过kill或killall命令杀死进程
[root@sky9890 sbin]# killall nginx
[root@sky9890 sbin]# kill 19640 #kill nginx主进程PID
查看端口号占用:
[root@sky9890 sbin]# netstat -tlnup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 18663/php-fpm
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1869/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 19657/nginx
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 18468/httpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1604/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2058/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 1181/dhclient
udp 0 0 172.19.68.202:123 0.0.0.0:* 1615/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 1615/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 1615/ntpd
优化配置:
创建软链接后,可以在任意目录下直接使用Nginx命令来控制Nginx服务。
[root@sky9890 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
用一个shell脚本实现Nginx服务
[root@sky9890 ~]# vi /etc/init.d/nginx
#!/bin/bash
#chkconfig: 35 85 15
DAEMON=/usr/local/nginx/sbin/nginx
case "$1" in
start)
echo "Starting nginx daemon..."
$ DAEMOM && echo "SUCCESS"
;;
stop)
echo "Stopping nginx daemon..."
$DAEMON -s quit && echo "SUCCESS"
;;
reload)
echo "Reloading nginx daemon...."
$DAEMON -s reload && echo "SUCCESS"
;;
restart)
echo "Restaring nginx daemon..."
$DAEMON -s quit
$DAEMON && echo "SUCESS"
;;
*)
echo "Usage:service nginx{start|stop|restart|reload}"
exit 2
;;
esac
[root@sky9890 ~]# chmod +x /etc/init.d/nginx #添加可执行权限
[root@sky9890 ~]# chkconfig --add nginx #添加自启动操作
[root@sky9890 ~]# chkconfig --level 2345 nginx on #设置启动级别
[root@sky9890 ~]# nginx -t #检查语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@sky9890 ~]# nginx -v #检测版本
nginx version: nginx/1.10.3
[root@sky9890 ~]# nginx -V
nginx version: nginx/1.10.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
原文地址:http://blog.51cto.com/sky9896/2104708