编译安装

下载源码:

1
2
3
wget http://nginx.org/download/nginx-<version>.tar.gz
tar -zxvf nginx-1.10.3.tar.gz
cd nginx-<version>

准备构建环境compiler,OpenSSL,PCRE(Perl Compatible Regular Expressions):

1
2
3
yum install -y gcc
yum install -y pcre-devel
yum install -y openssl openssl-devel

编译安装:

1
2
3
4
5
6
7
./configure \
    --with-http_ssl_module \
    --with-http_realip_module \
    --user=www \
    --group=www
make
make install

默认按在/usr/local/nginx目录下,可以使用configure参数--prefix 定制安装目录, 其他configure参数参考这里。 执行sbin/nginx -V可以打印安装时的编译参数。

创建nginx用户www:

1
2
3
groupadd www
useradd -g www -d /var/lib/www -s /sbin/nologin www
chown -R www:www /usr/local/nginx

启动与停止

/usr/lib/systemd/system/nginx.service

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/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=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
1
2
systemctl enable nginx
systemctl start nginx

参考