1、下载nginx最新版
wget http://nginx.org/download/nginx-1.12.0.tar.gz
2、解压&&进入
tar -xvzf nginx-1.12.0.tar.gz cd nginx-1.12.0
可以这样 安装依赖,也可以手动安装
yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel gd gd-devel
*******需要 Pcre 编译安装*******
wget https://downloads.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
缺少c++ 编译器,则安装
yum -y install gcc-c++
编译安装 Prce
./configure --prefix=/usr/local/prce-8.4 make && make install
*****需要 Openssl 编译安装****
wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz tar -zxvf openssl-1.0.21.tar.gz ./config --prefix=/usr/local/openssl ./config -t make make install
*********需要 zlib 库的安装*********
wget http://www.zlib.net/zlib-1.2.11.tar.gz tar -zxvf zlib-1.2.11.tar.gz ./configure --prefix=/usr/local/zlib make && make install
3、运行./configure脚本,指定安装目录
./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.40 --with-zlib=/usr/local/src/zlib-1.2.11 --with-openssl=/usr/local/openssl
编译安装的时候需要修改 nginx安装源下 auto/lib/openssl/conf openssl路径
make && make install
4、查看nginx进程和端口号
netstat -ntlp | grep nginx
5、nginx 加入service服务
编辑 /lib/systemd/system/nginx.service,
centos7 系统下 /usr/lib/systemd/system/nginx.service,
加入如下代码:
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/nginx/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
保存,退出;
设置文件权限 754, chmod -R 754 ./nginx.service
6、设置开机启动
systemctl enable nginx.service
如果之前启动过 nginx ,用 pkill -9 nginx 杀掉然后用
systemctl start nginx.service 的方式来启动nginx
7、配置nginx 支持 php
更改配置 编辑/usr/local/nginx/conf/nginx.conf
nginx 启动报错常见问题
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
解决:
nginx 增加模块
/usr/local/nginx/sbin/nginx -V 查看之前的编译参数
--user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipsub_module --with-openssl=/usr/local/lnmp1.4/src/openssl-1.0.2l
我们需要新增--with-http_image_filter_module 模块
在之前的编译参数之后 追加 --with-http_image_filter_module, 然后重新编译
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-openssl=/usr/local/lnmp1.4/src/openssl-1.0.2l --with-http_image_filter_module make cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak //备份 cp ./objs/nginx /usr/local/nginx/sbin/