linux下安装nginx以及常用命令指南

安装nginx之前,要先在服务器上安装nginx运行所需要的依赖包

目录选择:一般选择 "/usr/local/"

1.安装PCRE库

离线安装包:https://pan.baidu.com/s/1k_jDkGuMD5XRqDBz5MYhLg 密码:89nj

也可以使用wget在线下载安装包

1 cd /usr/local/
2 wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz 
3 tar -zxvf pcre-8.37.tar.gz
4 cd pcre-8.34
5 ./configure
6 make
7 make install

2.安装zlib库

离线安装包:https://pan.baidu.com/s/1198lSj48reV9vvjZjKJpHQ 密码:4nrl

也可以使用wget在线下载安装包

1 wget http://zlib.net/zlib-1.2.11.tar.gz
2 tar -zxvf zlib-1.2.11.tar.gz
3 cd zlib-1.2.11
4 ./configure
5 make
6 make install

3.安装openssl(如果之前已经安装,就不用重新装了)

离线安装包:https://pan.baidu.com/s/1V57vak0Z38fxPe3-JOTsSA 密码:8aig

也可以使用wget在线下载安装包

1 wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
2 tar -zxvf openssl-1.0.1t.tar.gz

上述依赖包安装完成后,继续安装nginx

离线安装包:https://pan.baidu.com/s/1AuV0q5qnX7lKoMggOFS5gg 密码:oytn

也可以使用wget在线下载安装包

1 wget http://nginx.org/download/nginx-1.1.10.tar.gz
2 tar -zxvf nginx-1.1.10.tar.gz
3 cd nginx-1.1.10
4 ./configure
5 make
6 make install

 安装完成后会在 /usr/local/ 下多出一个nginx目录(注意:nginx-1.9.1这个目录解压后的安装包,是个程序包目录,而安装完成nginx后会出现一个nginx目录)


1.未启动nginx时,进入nginx目录,如下图所示

2.启动后,nginx目录下会多出几个文件夹,如下图所示

启动后看到如下页面表示安装成功


nginx常用命令

1.启动命令

第一种方式:[root@localhost ~]# /usr/local/nginx/sbin/nginx

第二种方式:[root@localhost ~]# cd /usr/local/nginx/sbin  # 先切换到/sbin目录
          [root@localhost sbin]# ./nginx

(其实是一样的)

2.停止命令

第一种方式:[root@localhost sbin]# ./nginx -s stop   # 先切换到/sbin目录,然后使用该命令

第二种方式:[root@localhost sbin]# ./nginx -s quit # -s都是采用向 Nginx 发送信号的方式。

 3.修改nginx配置后,重新加载配置文件

第一种方式:[root@localhost sbin]# ./nginx -s reload
第二种方式:[root@localhost sbin]# /usr/local/nginx/sbin/nginx -s reload

4.显示帮助信息

[root@localhost sbin]# ./nginx -h
nginx version: nginx/1.9.1
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

[root@localhost sbin]#

 5.查看nginx正在运行

[root@localhost sbin]# ps -ef|grep nginx
root       3504      1  0 Jul03 ?        00:00:00 nginx: master process ./nginx
nobody     3729   3504  0 Jul03 ?        00:00:00 nginx: worker process
root       6315   2708  0 00:33 pts/0    00:00:00 grep --color=auto nginx
[root@localhost sbin]# 

这个时候也可以使用kill命令杀掉进程,然后重启nginx

6.查看nginx监听端口的状态

[root@localhost sbin]# netstat -npa|grep 80|grep nginx
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      3504/nginx: master  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3504/nginx: master  
[root@localhost sbin]# 

 一篇很全的帖子:http://www.nginx.cn/install

原文地址:https://www.cnblogs.com/hanmk/p/9258149.html