Centos8 编译安装Nginx

2020-08-1104:16:55
评论
1,659 2780字
Centos8 编译安装Nginx

关闭selinux 

sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config  && setenforce 0

# 准备安装文件

dnf install -y tar wget vim 
wget  https://oss.pealog.com/env/nginx/nginx-1.19.1.tar.gz
tar -zxvf nginx-1.19.1.tar.gz

# 进入安装目录

cd  nginx-1.19.1

## configure

./configure \
--prefix=/etc/nginx \
--with-pcre \
--with-http_auth_request_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-google_perftools_module  \
--with-http_geoip_module  \
--with-stream_ssl_module 

## 报错1 ::checking for C compiler … not found

dnf -y install gcc gcc-c++

## 报错2::./configure: error: the HTTP rewrite module requires the PCRE library.

dnf -y install pcre-devel

## 报错3::./configure: error: SSL modules require the OpenSSL library.

dnf -y install openssl openssl-devel

## 报错4::./configure: error: the GeoIP module requires the GeoIP library.

dnf -y install epel-release
dnf -y install GeoIP GeoIP-devel GeoIP-data

## 报错5::./configure: error: the Google perftools module requires the Google perftools

dnf -y install libunwind-devel google-perftools-devel

# 编译并安装

make && make install

## make未找到

dnf -y install automake autoconf libtool make

# 使systemctl能管理运行nginx服务

cd /etc/nginx/conf
mkdir /data
mkdir /data/nginx
mkdir /data/nginx/logs

## 创建nginx用户

#-g:指定所属的group

#-s:指定shell,因为它不需要登录,所以用/sbin/nologin

#-M:不创建home目录,因为它不需要登录

groupadd nginx
useradd -g nginx -s /sbin/nologin -M nginx

## vim nginx.conf

指定运行nginx的用户和组是:nginx

user nginx nginx;

## 发生错误时要写入到错误日志(目录用上面创建好的)

error_log   /data/nginx/logs/error.log;

## 指定pid的路径

pid        logs/nginx.pid;

## 日志格式(取消注释即可)

log_format  main

## 指定访问日志的路径和格式(目录用上面创建好的)

access_log  /data/nginx/logs/access.log  main;

# 生成service文件:

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

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

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

[Install]
WantedBy=multi-user.target

# 重新加载服务文件

systemctl daemon-reload

# 运行&自起Nginx服务

systemctl enable nginx && systemctl start nginx

# 放行端口

firewall-cmd --zone=public --add-port=80/tcp --permanent  
firewall-cmd --zone=public --add-port=443/tcp --permanent  
firewall-cmd --reload

# 常用命令

/etc/nginx/sbin/nginx -v          查看Nginx版本(小v)
/etc/nginx/sbin/nginx -V          查看nginx的配置编译参数(大V)
/etc/nginx/sbin/nginx             启动
/etc/nginx/sbin/nginx -s stop     停止
/etc/nginx/sbin/nginx -s reload   重启
/etc/nginx/sbin/nginx -t          检测语法错误
/etc/nginx/sbin/nginx -V 2>&1 | tr -- - '\n' | grep module # 查看安装了那些模块

# 快捷命令

systemctl start  nginx         #启动
systemctl stop   nginx         #停止
systemctl reload nginx         #重启
systemctl status nginx         #查看服务状态
豌豆日志
  • 本文由 发表于 2020-08-1104:16:55
  • 转载请务必保留本文链接:https://pealog.com/242.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: