操作系统:CentOS release 6.9 (Final)
软件: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 #ps命令查看Nginx运行状态
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
[root@sky9890 ~]# nginx -s quit #停止Nginx服务
[root@sky9890 ~]# nginx #启动Nginx服务
[root@sky9890 ~]# nginx -s reload #重新载入配置(平滑启动)
用一个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
附:配置源码:
[root@sky9890 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
index inex.php;
location ~ .*\.(php|php5)?$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
error_page 500 502 503 504 403 404 https://blog.51cto.com/sky9896;
}