欧阳简之 > centos7.4 源码安装 nginx

热爱代码,对每一行代码保持一颗敬畏之心。

安装环境:CentOS Linux release 7.4 (Core)

安装依赖库

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

下载源码

官网下载直接下载.tar.gz安装包地址:https://nginx.org/en/download.html

321.jpg

这里我们选择稳定版本:1.14.0

cd /root/
wget -c https://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0

编译安装

./configure --prefix=/data/app/nginx \
    --pid-path=/data/app/nginx/run/nginx.pid \
    --user=nginx \
    --group=nginx \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_realip_module \
    --with-pcre

make && make install

配置

创建nginx用户组及用户

groupadd nginx
useradd -g nginx nginx

设置Nginx目录权限

chown -R nginx.nginx /data/app/nginx

配置nginx日志切割

cat > /etc/logrotate.d/nginx << EOF
/data/app/nginx/logs/*.log {
    create 0644 nginx nginx
    daily
    rotate 30
    dateext
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 \`cat /data/app/nginx/run/nginx.pid 2>/dev/null\` 2>/dev/null || true
    endscript
}
EOF

配置nginx服务

cat > /lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - A very fast and reliable nginx engine
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
LimitNOFILE=655360
LimitNPROC=655360
PIDFile=/data/app/nginx/run/nginx.pid
ExecStartPre=/data/app/nginx/sbin/nginx -t -c /data/app/nginx/conf/nginx.conf
ExecStart=/data/app/nginx/sbin/nginx
ExecReload=/data/app/nginx/sbin/nginx -s reload
ExecStop=/data/app/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
EOF

配置完后,我们即可使用下列命令来管理nginx服务了

systemctl stop nginx.service   停止nginx服务
systemctl start nginx.service  启动nginx服务
systemctl status nginx.service  查看nginx状态
systemctl restart nginx.service  重启nginx服务
systemctl reload nginx.service  重载nginx服务
systemctl enable nginx.service  开机自启动
systemctl disable nginx.service  禁止开机自启动

启动

systemctl start nginx.service

访问IP,安装成功
333.jpg

tagged by none