1、检测Nginx是否安装
ps -ef | grep nginx
确保进行了安装了linux常用必备支持库。检查是否安装了g++、gcc。rpm -qa | grep gcc 之后需要出现3个包如下图所示。如果没有出现。需要安装g++、gcc。
rpm -qa | grep gcc
yum install gcc-c++
判断系统是否安装了zlib-devel。如果没有安装。使用
rpm -qa | grep zlib-devel
yum install -y zlib-devel
2、获取nginx最新稳定的版本 我这里使用nginx-1.10.2.tar.gz
wget http://nginx.org/download/nginx-1.10.2.tar.gz
3、解压nginx-1.10.2.tar.gz
4、设置一下配置信息 ./configure --prefix=/usr/local/nginx,(通常将软件安装在/usr/local/目录下)或者不执行此步,直接默认配置
cd nginx-1.10.2/
安装pcre开发包
#yum install -y pcre-devel
如果安装出现在下面的错误是缺少编译环境。安装编译源码所需的工具和库
./configure: error: C compiler cc is not found
#yum install gcc gcc-c++ ncurses-devel perl
安装cmake,从http://www.cmake.org下载源码并编译安装
#yum -y install make gcc gcc-c++ ncurses-devel
#yum -y install zlib zlib-devel
如果需要ssl功能需要openssl库
#yum -y install openssl openssl-devel
5、编译安装
make 编译 (make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件)
make install 安装 (make install是把这些编译出来的可执行文件和库文件复制到合适的地方)
启动操作
/usr/local/nginx/sbin/nginx (/usr/local/nginx/sbin/nginx -t查看配置信息是否正确)
#方法1
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#方法2
# cd /usr/local/nginx/sbin
# ./nginx
停止操作停止操作是通过向nginx进程发送信号来进行的
步骤1:查询nginx主进程号
ps -ef | grep nginx
在进程列表里面找master进程,它的编号就是主进程号了。
步骤2:发送信号
nginx -s stop :快速停止nginx
quit :完整有序的停止nginx
从容停止Nginx:
kill -QUIT 主进程号
快速停止Nginx:
kill -TERM 主进程号
强制停止Nginx:
pkill -9 nginx
另外, 若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文 件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:
kill -信号类型 '/usr/local/nginx/logs/nginx.pid'
平滑重启
如果更改了配置就要重启Nginx,要先关闭Nginx再打开?不是的,可以向Nginx发送信号,平滑重启。
平滑重启命令:
kill -HUP 住进称号或进程号文件路径
或者使用
/usr/local/nginx/sbin/nginx -s reload
注意,修改了配置文件后最好先检查一下修改过的配置文件是否正确,以免重启后Nginx出现错误影响服务器稳定运行。判断Nginx配置是否正确命令如下:
nginx -t -c /usr/local/nginx/conf/nginx.conf
或者
/usr/local/nginx/sbin/nginx -t
nginx -s reload :修改配置后重新加载生效
nginx -s reopen :重新打开日志文件
nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确
基本操作命令
nginx -h #帮助
nginx -v #显示版本
nginx -V #显示版本和配置信息
nginx -t #测试配置
nginx -q #测试配置时,只输出错误信息
nginx -s stop #停止服务器
nginx -s reload #重新加载配置
测试
其他:
查看端口占用情况
#netstat -tunlp
详细配置在其他文章中描述
补充:
1、准备工作
选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL。
Nginx是C写的,需要用GCC编译;Nginx的Rewrite和HTTP模块会用到PCRE;Nginx中的Gzip用到zlib;
用命令“# gcc”,查看gcc是否安装;如果出现“gcc: no input files”信息,说明已经安装好了。
否则,就需要用命令“# yum install gcc”,进行安装了!一路可能需要多次输入y,进行确认。
安装好后,可以再用命令“#gcc”测试,或者用命令“# gcc -v”查看其版本号。
同样方法,用如下命令安装PCRE,zlib,OpenSSL(其中devel,是develop开发包的意思):
1. # yum install -y pcre pcre-devel
2. # yum install -y zlib zlib-devel
3. # yum install -y openssl openssl-devel
2、下载并安装
创建目录(nginx-src)并进去;然后,从官方地址(http://nginx.org/)下载,解压,配置,编译,安装:
1. # mkdir nginx-src && cd nginx-src
2. # wget http://nginx.org/download/nginx-1.7.3.tar.gz
3. # tar xzf nginx-1.7.3.tar.gz
4. # cd nginx-1.7.3
5. # ./configure
6. # make
7. # make install
8. # whereis nginx
9. nginx: /usr/local/nginx
默认的安装路径为:/usr/local/nginx;跳转到其目录下sbin路径下,便可以启动或停止它了。
1. # ./nginx -h
2. nginx version: nginx/1.7.3
3. Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
4. Options:
5. -?,-h : this help
6. -v : show version and exit
7. -V : show version and configure options then exit
8. -t : test configuration and exit
9. -q : suppress non-error messages during configuration testing
10. -s signal : send signal to a master process: stop, quit, reopen, reload
11. -p prefix : set prefix path (default: /usr/local/nginx/)
12. -c filename : set configuration file (default: conf/nginx.conf)
13. -g directives : set global directives out of configuration file
启动:nginx
停止:nginx -s stop
3、添加到系统服务
使用命令“# vi /etc/init.d/nginx”,打开编辑器,输入如下内容:
1. #!/bin/sh
2. # chkconfig: 2345 85 15
3. # Startup script for the nginx Web Server
4. # description: nginx is a World Wide Web server.
5. # It is used to serve HTML files and CGI.
6. # processname: nginx
7. # pidfile: /usr/local/nginx/logs/nginx.pid
8. # config: /usr/local/nginx/conf/nginx.conf
9.
10. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
11. DESC="nginx deamon"
12. NAME=nginx
13. DAEMON=/usr/local/nginx/sbin/$NAME
14. SCRIPTNAME=/etc/init.d/$NAME
15.
16. test -x $DAEMON || exit 0
17.
18. d_start(){
19. $DAEMON || echo -n "already running"
20. }
21.
22. d_stop(){
23. $DAEMON -s quit || echo -n "not running"
24. }
25.
26.
27. d_reload(){
28. $DAEMON -s reload || echo -n "can not reload"
29. }
30.
31. case "$1" in
32. start)
33. echo -n "Starting $DESC: $NAME"
34. d_start
35. echo "."
36. ;;
37. stop)
38. echo -n "Stopping $DESC: $NAME"
39. d_stop
40. echo "."
41. ;;
42. reload)
43. echo -n "Reloading $DESC conf..."
44. d_reload
45. echo "reload ."
46. ;;
47. restart)
48. echo -n "Restarting $DESC: $NAME"
49. d_stop
50. sleep 2
51. d_start
52. echo "."
53. ;;
54. *)
55. echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2
56. exit 3
57. ;;
58. esac
59.
60. exit 0
保存退出后,再使用下面的命令,使其可执行;然后,添加配置并查看。
可用chkconfig修改其值,也可用ntsysv工具改变是否自启动。
1. # chmod +x /etc/init.d/nginx
2. # chkconfig --add nginx
3. # chkconfig nginx on/off
4. # chkconfig --list nginx
5. nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
* nginx作为服务 启动、停止、无间断服务重启操作
# service nginx start
# service nginx stop
# service nginx reload
[root@localhost ~]# chkconfig --list 显示开机可以自动启动的服务
[root@localhost ~]# chkconfig --add *** 添加开机自动启动***服务
[root@localhost ~]# chkconfig --del *** 删除开机自动启动***服务