/--- date: 2017-01-09 20:34:43 +0800 categories: 技术 ---/

LEMP环境搭建(二)nginx

编译安装

the default build configuration depends on 3 libraries to be installed:

These libraries can be installed from source or through the package manager.

  1. 安装configure选项配置依赖包

    yum install -y gcc pcre pcre-devel openssl openssl-devel zlib
  2. configure选项配置,配置参数说明官方文档

    wget https://nginx.org/download/nginx-1.12.2.tar.gz
    tar -zxvf nginx-1.12.2.tar.gz
    cd nginx-1.12.2
    ./configure --prefix=/usr/local/nginx \
     --http-client-body-temp-path=/usr/local/nginx/temp/client_body_temp\
     --http-proxy-temp-path=/usr/local/nginx/temp/proxy_temp\
     --http-fastcgi-temp-path=/usr/local/nginx/temp/fastcgi_temp\
     --http-uwsgi-temp-path=/usr/local/nginx/temp/uwsgi_temp\
     --http-scgi-temp-path=/usr/local/nginx/temp/scgi_temp\
     --user=nginx \
     --group=www \
     --with-http_ssl_module \
     --with-http_v2_module \
     --with-http_realip_module \
     --with-http_stub_status_module
  3. 编译、安装

    make
    make install

相关conf、用户、日志目录配置

mkdir -p /usr/local/nginx/temp
useradd -g www -M -s /sbin/nologin -N nginx #添加用户
vim /usr/local/nginx/conf/nginx.conf  #配置用户和用户组,日志等,并在server指令前加入 include conf.d/*.conf;
mkdir -p /usr/local/nginx/conf/conf.d

加入systemctl控制,启动、关闭、查看状态

vim /lib/systemd/system/nginx.service #写入以下内容(去掉前面#号)

# [Unit]
# Description=The NGINX HTTP and reverse proxy server
# After=syslog.target network.target remote-fs.target nss-lookup.target
# 
# [Service]
# Type=forking
# PIDFile=/usr/local/nginx/logs/nginx.pid
# ExecStartPre=/usr/local/nginx/sbin/nginx -t
# ExecStart=/usr/local/nginx/sbin/nginx
# ExecReload=/bin/kill -s HUP $MAINPID
# ExecStop=/bin/kill -s QUIT $MAINPID
# PrivateTmp=true
# 
# [Install]
# WantedBy=multi-user.target

systemctl enable nginx.service
systemctl start nginx.service
systemctl restart nginx.service
systemctl stop nginx.service
systemctl status nginx.service

配置防火墙

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https #如果需要https服务
firewall-cmd --reload

path环境变量设置

vim /etc/profile
#在文档最后,添加:
# export PATH="$PATH:/usr/local/nginx/sbin "
#或
# export PATH="/usr/local/nginx/sbin:$PATH"
#保存,退出,然后运行:
source /etc/profile

附1:参考

https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

附2:其他命令

nginx -h
nginx -V
nginx -t
nginx -c /home/user/nginx.conf
nginx 
ps aux | grep nginx
curl 127.0.0.1

附3:CGI && FastCGI && PHP-FPM && 《HTTP权威指南》