Centos8安装Nginx1.18.0

nginx 官方网站下载对应版本包

http://nginx.org/en/download.html

安装Nginx依赖包

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
安装PCRE

PCRE 作用是让 Nginx 支持 Rewrite 功能。
yum install -y pcre pcre-devel

编译安装nginx

[root@localhost nginx-1.18.0]# mkdir /usr/local/nginx-1.18.0
[root@localhost opt]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@localhost opt]# tar -zxvf nginx-1.18.0.tar.gz
[root@localhost opt]# cd nginx-1.18.0/
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.18.0]# make
[root@localhost nginx-1.18.0]# make install
root@localhost nginx-1.18.0]# /usr/local/nginx-1.18.0/sbin/nginx -v
nginx version: nginx/1.18.0

指定编译安装目录 --prefix=/usr/local/nginx-1.18.0
监控模块 --with-http_stub_status_module
SSL模块 --with-http_ssl_module模块

配置nginx.conf 文件

groupadd nginx
useradd -g nginx -s /sbin/nologin -M nginx 
    #-g:指定所属的group
    #-s:指定shell,因为它不需要登录,所以用/sbin/nologin
    #-M:不创建home目录,因为它不需要登录
cd /usr/local/nginx-1.18.0/conf/
vi nginx.conf
#指定nginx用户和组
user nginx nginx;
#错误日志存放目录
error_log  logs/error.log;

#指定pid的路径
pid logs/nginx.pid;
#日志格式(取消注释即可)
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
#指定访问日志的路径和格式
access_log  logs/access.log main

配置systemctl 管理

[root@localhost nginx-1.18.0]# vim /usr/lib/systemd/system/nginx.service
添加以下内容

[Unit]
Description=nginx-The High-performance HTTP Server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx-1.18.0/logs/nginx.pid
ExecStartPre=/usr/local/nginx-1.18.0/sbin/nginx -t -c /usr/local/nginx-1.18.0/conf/nginx.conf
ExecStart=/usr/local/nginx-1.18.0/sbin/nginx -c /usr/local/nginx-1.18.0/conf/nginx.conf
ExecReload=/usr/local/nginx-1.18.0/sbin/nginx -s reload
ExecStop=/usr/local/nginx-1.18.0/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

systemctl启动nginx

重新加载服务文件
systemctl daemon-reload
systemctl start nginx.service
设置nginx开机自启
systemctl enable nginx.service
不自启
systemctl disable nginx.service
#查看所有已启动服务
systemctl list-units --type=service
原文地址:https://www.cnblogs.com/wangchengyi/p/13831374.html