CentOS8 安装 Nginx 1.18.0 tar包安装

CentOS8安装Nginx

1. 安装nginx 需要的环境依赖

#需要gcc环境 前面安装过
yum install gcc-c++

yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

2. 安装nginx1.18.0

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
./configure	#使用默认配置
make
make install
/usr/local/nginx/sbin/nginx -v	#测试成功

3. 配置nginx.conf 文件

#配置日志目录
mkdir -p /home/hanhuibing/workspace/data/nginx/logs
#创建nginx用户
groupadd nginx
useradd -g nginx -s /sbin/nologin -M nginx 
    #-g:指定所属的group
    #-s:指定shell,因为它不需要登录,所以用/sbin/nologin
    #-M:不创建home目录,因为它不需要登录
vi /conf/nginx.conf
#指定nginx用户和组
user nginx nginx;
#错误日志存放目录
error_log  /home/hanhuibing/workspace/data/nginx/logs/nginx-error.log;
			/usr/local/nginx/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  /home/hanhuibing/workspace/data/nginx/logs/access.log main

4. 生成系统service管理文件

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/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=/usr/local/nginx/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
#访问http://192.168.1.100/ 成功

你可能感兴趣的:(产品安装配合-运维,nginx)