Nginx 安装

> 安装环境为 centos 7.5

1. 下载源码包

```shell
mkdir /tmp/nginx_build && cd /tmp/nginx_build
wget http://nginx.org/download/nginx-1.14.0.tar.gz
```

2. 安装编译依赖

```bash
yum update -y
yum install -y gcc-c++ pcre-devel openssl-devel
# file system 服务器 需要安装此包, 用于 nginx 图片处理(一般 nginx + php 不需安装此包)
yum install -y gd-devel
```

3. 创建 nginx 用户组

```
useradd -r -M nginx
```

4. 预编译 nginx

```shell
# 创建 nginx 缓存目录
mkdir -p /var/cache/nginx/client_temp /var/cache/nginx/proxy_temp /var/cache/nginx/fastcgi_tem /var/cache/nginx/uwsgi_temp /var/cache/nginx/scgi_temp
chown -R nginx:nginx /var/cache/nginx

# nginx 安装目录
NG_DIR=/usr/local/nginx-1.14.0

tar zxvf nginx-1.14.0.tar.gz && cd nginx-1.14.0
# 预编译
./configure --prefix=$NG_DIR
--sbin-path=$NG_DIR/sbin/nginx
--modules-path=$NG_DIR/modules
--conf-path=$NG_DIR/conf/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=${NG_DIR}/run/nginx.pid
--lock-path=${NG_DIR}/run/nginx.lock
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
--user=nginx
--group=nginx
--with-compat
--with-file-aio
--with-threads
--with-http_addition_module
--with-http_auth_request_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_realip_module
--with-http_secure_link_module
--with-http_slice_module
--with-http_ssl_module
--with-http_stub_status_module
--with-http_sub_module
--with-http_v2_module
--with-stream
--with-stream_realip_module
--with-stream_ssl_module
--with-stream_ssl_preread_module

# 如果 nginx 作为 file system 请添加此编译参数, 让 nginx 支持 图片处理
--with-http_image_filter_module
```

5. 编译安装

```shell
make -j `grep processor /proc/cpuinfo | wc -l` && make install

# 创建标准配置目录
ln -s /usr/local/nginx-1.14.0/conf /etc/nginx

# 创建二进制文件连接
ln -s /usr/local/nginx-1.14.0/sbin/nginx /usr/sbin
```

6. 开机启动 nginx

```bash
# nginx system unit file
cat > /usr/lib/systemd/system/nginx.service <<-'EOF'
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx-1.14.0/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
EOF

# 开机自启动
systemctl enable nginx && systemctl start nginx

```

原文地址:https://www.cnblogs.com/Christine-ting/p/10697726.html