nginx可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息。
正式开始前,编译环境gcc g++ 开发库之类的需要提前装好,这里默认你已经装好。
centos平台编译环境使用如下指令
安装make:
1 | yum -y install gcc automake autoconf libtool make |
安装g++:
1 | yum install gcc gcc-c++ |
下面正式开始
---------------------------------------------------------------------------
一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。
1.选定源码目录
可以是任何目录,本文选定的是/usr/local/src
1 | cd /usr/local/src |
2.安装PCRE库
https://sourceforge.net/projects/pcre/files/pcre/8.43/pcre-8.43.tar.gz/download 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
1 2 3 4 5 6 7 | cd /usr/local/src tar -zxvf pcre-8.37.tar.gz cd pcre-8.34 ./configure make make install |
3.安装zlib库
http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:
1 2 3 4 5 6 7 8 | cd /usr/local/src tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure make make install |
4.安装ssl(某些vps默认没装ssl)
https://www.openssl.org/source/下载最新的 SSL源码包,使用下面命令下载编译和安装 SSL包:
1 2 3 4 5 | cd /usr/local/src tar -zxvf openssl-1.0.1t.tar.gz ./configure 如果./confiure 就是用./config make make install |
5.安装nginx
Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:(下载地址:http://nginx.org/download/)
1 2 3 4 5 6 7 | cd /usr/local/src tar -zxvf nginx-1.4.2.tar.gz cd nginx-1.4.2 ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.34 --with-zlib=/usr/local/src/zlib-1.2.8 --with-openssl=/usr/local/src/openssl-1.0.1t --with-http_ssl_module make make install |
启动、停止nginx
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
./nginx -s quit
:此方式停止步骤是待nginx进程处理任务完毕进行停止。./nginx -s stop
:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
查看是否启动成功
1:ps -ef | grep nginx 就可以看到Nginx进程是否存在了
2: 如果我们的Nginx运行在80端口,那么就可以通过netstat -anp | grep :80命令来判断Nginx是否启动(如何netstat 命令无效也需要安装:yum install net-tools)
3:打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
问题:
1:安装过程中出现:错误提示:./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=<path> options.
解决办法:
yum -y install openssl openssl-devel
2:nginx启动成功了,但是还是访问不了
解决办法:应该是80端口没有开放,开放80端口
vi /etc/sysconfig/iptables 打开配置文件加入如下语句:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j DROP 重启防火墙,修改完成
:wq! 保存退出
service iptables restart 重启防火墙,修改生效
环境变量设置
下来配置环境变量
在/etc/profile 中加入:
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
保存,
执行 source /etc/profile ,使配置文件生效
接下来就可以在任意位置使用nginx命令了。