Linux环境下安装Nginx-sky9890
操作系统:CentOS> 软件:nginx-1.10.3任务:安装 Nginx
实战技巧:设置nginx自动启动服务
1.获取Nginx软件
#
wget http://nginx.org/download/nginx-1.10.3.tar.gz
# tar -zxvf nginx-1.10.3.tar.gz
# cd nginx-1.10.3
# ls
autoCHANGESCHANGES.ruconfconfigurecontribhtmlLICENSEMakefilemanobjsREADMEsrc
2.编译安装Nginx
安装依赖包:
# yum install pcre-devel zlib-devel openssl-devel
#./configure \ #配置
--prefix=/usr/local/nginx \
--with-http_ssl_module
#make && make install #编译与安装
3.Nginx的启动与停止
Nginx启动
# cd /usr/local/nginx/sbin/
# ./nginx
# ps aux|grep ninx#ps命令查看Nginx运行状态
root 195990.00.0 103336 900 pts/0 S+ 08:38 0:00 grep ninx
立即停止服务:
# ./nginx-s stop
从容停止服务:
# ./nginx-s quit
通过kill或killall命令杀死进程
# killall nginx
# kill 19640 #kill nginx主进程PID
查看端口号占用:
# 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服务。
# ln -s /usr/local/nginx/sbin/nginx/usr/local/sbin/nginx
# nginx -s quit#停止Nginx服务
# nginx #启动Nginx服务
# nginx -s>#重新载入配置(平滑启动)
用一个shell脚本实现Nginx服务:
# 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"
;;
> echo "Reloading nginx daemon...."
$DAEMON -s> ;;
restart)
echo "Restaring nginx daemon..."
$DAEMON -s quit
$DAEMON && echo "SUCESS"
;;
*)
echo "Usage:service nginx{start|stop|restart|reload}"
exit 2
;;
esac
# chmod +x /etc/init.d/nginx #添加可执行权限
# chkconfig --add nginx #添加自启动操作
# chkconfig --level 2345 nginx on #设置启动级别
# 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
# nginx -v #检测版本
nginx version: nginx/1.10.3
# 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
附:配置源码:
# cat nginx.conf
worker_processes1;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
server_tokens off;
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_loglogs/access.logmain;
sendfile on;
keepalive_timeout65;
gzipon;
server {
listen 80;
server_namelocalhost;
indexinex.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 http://blog.51cto.com/sky9896;
}
页:
[1]