Linux 安装nginx服务器详细介绍

nginx依赖一些软件库,在安装之前请确保系统安装了gcc、ssl、pcre和gzip等软件,可以用rpm -q 命令查看软件是否安装。

[root@RedHat1 ~]# rpm -q gcc
gcc-4.1.2-44.el5

依赖库信息如下:

(1). gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/ )
(2). rewrite 模块需要 pcre 库 ( 下载: http://www.pcre.org/)
(3). ssl 功能需要 openssl 库 ( 下载: http://www.openssl.org/)

如安装pcre,下载pcre至目的目录下,这里选择的版本是pcre-8.38,下载完后执行以下操作

  tar -zxvf pcre-8.38.tar.gz  cd pcre-8.38  ./configure  make  make install

安装nginx,执行如下命令

       在默认情况下,经过编译安装的Nginx已经包含了大部分可用模块,可以通过“./configure  --help”选项设置各个模块的使用情况,例如对不需要的http_ssi模块,可通过“--without-http_ssi_module”参数关闭此模块;如果需要“http_perl”模块,则可以通过“--with-http_perl_module”参数安装此模块。执行以下操作进行安装。

  tar -zxvf nginx-1.11.1.tar.gz  cd nginx-1.11.1  ./configure --with-pcre=../pcre-8.38 --prefix=/usr/local/nginx  make  make install

检测是否安装成功,执行命令如下

[root@RedHat1 sbin]# cd /usr/local/nginx/sbin
[root@RedHat1 sbin]# ./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

[root@RedHat1 sbin]# ./nginx

查看端口

[root@RedHat1 sbin]# netstat -ntlp

结果如下:

Proto Recv-Q Send-Q Local Address     Foreign Address   State    PID/Program name
tcp        0      0 127.0.0.1:2208    0.0.0.0:*         LISTEN   2993/hpiod
tcp        0      0 0.0.0.0:834       0.0.0.0:*         LISTEN   2772/rpc.statd
tcp        0      0 0.0.0.0:11111     0.0.0.0:*         LISTEN   3391/ricci
tcp        0      0 0.0.0.0:111       0.0.0.0:*         LISTEN   2733/portmap
tcp        0      0 0.0.0.0:80        0.0.0.0:*         LISTEN   3852/nginx
tcp        0      0 0.0.0.0:16851     0.0.0.0:*         LISTEN   3290/modclusterd
tcp        0      0 127.0.0.1:631     0.0.0.0:*         LISTEN   3024/cupsd
tcp        0      0 127.0.0.1:25      0.0.0.0:*         LISTEN   3057/sendmail: acce
tcp        0      0 127.0.0.1:2207    0.0.0.0:*         LISTEN   2998/python
tcp        0      0 :::22             :::*              LISTEN   3013/sshd

也可以在浏览器中输入:http://localhost来验证是否启动成功。

停止nginx

停止操作是通过向nginx进程发送信号来进行的

步骤1:查询nginx主进程号:

ps -ef | grep nginx

在进程列表里 面找master进程,它的编号就是主进程号了。

步骤2:发送信号

从容停止Nginx:kill -QUIT 主进程号
快速停止Nginx:kill -TERM 主进程号
强制停止Nginx:pkill -9 nginx

重启nginx:平滑重启

        如果更改了配置就要重启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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

参与评论