【Nginx应用】2.在Linux中搭建Nginx
上一篇介绍了正反向代理以及负载均衡的概念,下面来正式学习Nginx。
首先从搭建环境开始。这里选择的是安装了CentOS的VMware演示搭建过程。
1.下载压缩包
我们要将软件安装在Linux系统的"/usr/local"目录下,所以首先
将目录切换至"/usr/local"下,然后使用"wget"方式获取Nginx的压缩包,具体
获取指令为:wget http://nginx.org/download/nginx-1.5.9.tar.gz

如上图,当进度条为100%时说明下载完毕。
2.解压安装
下载完成之后,对文件进行解压:
tar -zxvf nginx-1.5.9.tar.gz
解压完毕后,进入解压后的目录,进行编译:

在编译过程中,可能会报错,这是因为所需依赖环境不足导致的。
一般Nginx会依赖gc++库和PCRE库,以及OpenSSL的md5功能、zlib等。
所以报以上库缺失的时候,要安装相关库:
(1)报缺失gc++库
在线安装gcc gcc-++:
yum -y install gcc gcc-++ autoconf automake
(2)报./configure: error: the HTTP rewrite module requires the PCRE library.
在线安装PCRE库:
yum -y install pcre-devel
(3)报./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.
安装OpenSSL库,使其拥有md5功能:
yum -y install openssl openssl-devel
(4)报./configure: error: the HTTP gzip module requires the zlib library.
安装“zlib-devel”:
yum install -y zlib-devel
以上依赖安装完毕后,继续执行“./configure --prefix=/usr/local/nginx”即可完成编译。
出现以下信息时,说明编译成功:

编译完成之后进行install安装:
make & make install
3.启动Nginx
当Nginx安装成功之后,使用以下格式的指令就可以启动它:
“【Nginx启动文件地址】 -c 【Nginx配置文件地址】”
正如本例中,是将Nginx安装在/usr/local/nginx,所以启动命令为:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
启动完毕之后,使用“ps -ef | grep nginx”查看服务状态:

4.关闭Nginx
有三种方式(其中9099是我们测试服务的端口号):
从容停止 : Kill -QUIT 9099
快速停止 : kill -TERM 9099 或 kill -INT 9099
强制停止 : pkill -9 nginx
5. 重启
第一种reload命令:
[[email protected] local]# cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx -s reload
第二种发送信号方式:
kill -HUP 13421
首先从搭建环境开始。这里选择的是安装了CentOS的VMware演示搭建过程。
1.下载压缩包
我们要将软件安装在Linux系统的"/usr/local"目录下,所以首先
将目录切换至"/usr/local"下,然后使用"wget"方式获取Nginx的压缩包,具体
获取指令为:wget http://nginx.org/download/nginx-1.5.9.tar.gz
如上图,当进度条为100%时说明下载完毕。
2.解压安装
下载完成之后,对文件进行解压:
tar -zxvf nginx-1.5.9.tar.gz
解压完毕后,进入解压后的目录,进行编译:
在编译过程中,可能会报错,这是因为所需依赖环境不足导致的。
一般Nginx会依赖gc++库和PCRE库,以及OpenSSL的md5功能、zlib等。
所以报以上库缺失的时候,要安装相关库:
(1)报缺失gc++库
在线安装gcc gcc-++:
yum -y install gcc gcc-++ autoconf automake
(2)报./configure: error: the HTTP rewrite module requires the PCRE library.
在线安装PCRE库:
yum -y install pcre-devel
(3)报./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.
安装OpenSSL库,使其拥有md5功能:
yum -y install openssl openssl-devel
(4)报./configure: error: the HTTP gzip module requires the zlib library.
安装“zlib-devel”:
yum install -y zlib-devel
以上依赖安装完毕后,继续执行“./configure --prefix=/usr/local/nginx”即可完成编译。
出现以下信息时,说明编译成功:
编译完成之后进行install安装:
make & make install
3.启动Nginx
当Nginx安装成功之后,使用以下格式的指令就可以启动它:
“【Nginx启动文件地址】 -c 【Nginx配置文件地址】”
正如本例中,是将Nginx安装在/usr/local/nginx,所以启动命令为:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
启动完毕之后,使用“ps -ef | grep nginx”查看服务状态:
4.关闭Nginx
有三种方式(其中9099是我们测试服务的端口号):
从容停止 : Kill -QUIT 9099
快速停止 : kill -TERM 9099 或 kill -INT 9099
强制停止 : pkill -9 nginx
5. 重启
第一种reload命令:
[[email protected] local]# cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx -s reload
第二种发送信号方式:
kill -HUP 13421
Nginx的安装到这里就结束了,以后的章节会对其使用进行详细介绍。
转载请注明出处:http://blog.****.net/acmman/article/details/78079898