Linux 安装Nginx详细图解教程

来源:互联网 发布:拍拍微店官网软件 编辑:程序博客网 时间:2024/06/05 05:00

一、(Linux查看当前操作系统版本信息)(系统:Centos7 64位 或者Red Hat 4.4.6-4)

cat /proc/version

Linux version 2.6.32-279.el6.i686 (mockbuild@x86-010.build.bos.redhat.com) (gcc version 4.4.6 20120305 (Red Hat 4.4.6-4)下面的操作在centos7同样适用

Nginx: http://nginx.org/en/download.html 我下载1.10.3

下载模块依赖性Nginx需要依赖下面3个包

1.gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/ )
2.rewrite 模块需要 pcre 库 ( 下载: http://www.pcre.org/ )
3.ssl 功能需要 openssl 库 ( 下载: http://www.openssl.org/ )
依赖包安装顺序依次为:openssl、zlib、pcre, 然后安装Nginx包
如果没有安装c++编译环境,还得安装,通过yum install gcc-c++完成安装
下载完成后:

下一步,编译安装

openssl :

[root@localhost] tar zxvf openssl-fips-2.0.16.tar.gz[root@localhost] cd openssl-fips-2.0.16[root@localhost] ./config && make && make install

pcre:

[root@localhost] tar zxvf pcre2-10.30.tar.gz[root@localhost] cd pcre2-10.30[root@localhost]  ./configure && make && make install

Zlib :

zl[root@localhost]tar zxvf zlib-1.2.11.tar.gz [root@localhost] cd zlib-1.2.11 [root@localhost] ./configure && make && make install 

后安装nginx:

[root@localhost]tar zxvf nginx-1.8.0.tar.gz[root@localhost] cd nginx-1.8.0[root@localhost]  ./configure && make && make install

发现报错了,错误为:./configure: error: the HTTP rewrite module

requires the PCRE library.
安装pcre-devel解决问题

yum -y install pcre-devel

还有可能出现:
错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
–without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
–with-http_ssl_module –with-openssl= options.
解决办法:

yum -y install openssl openssl-devel

安装完成后,可启动nginx:

启动命令:/usr/local/nginx/sbin/nginx
发现报错了:
error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
经网上查询,这是linux的通病

[root@localhost nginx]# sbin/nginx 

sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[root@localhost nginx]# error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[root@localhost nginx]# whereis libpcre.so.1
libpcre.so: /lib64/libpcre.so.0 /usr/local/lib/libpcre.so /usr/local/lib/libpcre.so.1
[root@localhost nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[root@localhost nginx]# sbin/nginx
先找到libpcre.so.1所在位置,然后做个软链接就可以了。
查看是否已启动:
通过浏览器访问:
看到这个就说明nginx安装并启动成功。
ps:
启动:/usr/local/nginx/sbin/nginx
停止/重新加载:/usr/local/nginx/sbin/nginx -s stop(quit、reload)
验证配置文件是否合法:/usr/local/nginx/sbin/nginx -t
命令帮助:/usr/local/nginx/sbin/nginx -h

其他参考:

安装后在linux下启动和关闭nginx:
启动操作
/usr/nginx/sbin/nginx (/usr/nginx/sbin/nginx -t 查看配置信息是否正确)
停止操作
停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的
步骤1:查询nginx主进程号
ps -ef | grep nginx
在进程列表里 面找master进程,它的编号就是主进程号了。
步骤2:发送信号
从容停止Nginx:
kill -QUIT 主进程号
快速停止Nginx:
kill -TERM 主进程号
强制停止Nginx:
pkill -9 nginx
另外, 若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文 件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:
kill -信号类型 ‘/usr/nginx/logs/nginx.pid’
平滑重启
如果更改了配置就要重启Nginx,要先关闭Nginx再打开?不是的,可以向Nginx 发送信号,平滑重启。
平滑重启命令:
kill -HUP 住进称号或进程号文件路径
或者使用
/usr/nginx/sbin/nginx -s reload
注意,修改了配置文件后最好先检查一下修改过的配置文件是否正 确,以免重启后Nginx出现错误影响服务器稳定运行。判断Nginx配置是否正确命令如下:
nginx -t -c /usr/nginx/conf/nginx.conf
或者
/usr/nginx/sbin/nginx -t
如下:
[root@item8 nginx]# 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

原文:
http://blog.csdn.net/hybaym/article/details/50929958

原创粉丝点击