openEuler Linux 源代码编译安装 Nginx,


openEuler Linux 源代码编译安装 Nginx

升级系统和软件

yum -y update

安装依赖

yum -y groupinstall Development Tools
yum -y install gcc gcc-c++
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
yum -y install vim net-tools man wget tar

或者:

yum -y install gcc gcc-c++ make cmake zlib zlib-devel openssl openssl-devel pcre-devel vim net-tools man wget tar

gcc gcc-c++编译环境
gzip 模块需要 zlib 库
rewrite 模块需要 pcre 库
ssl 功能需要openssl库

添加nginx组,用户

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

下载安装包

wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar -zxvf nginx-1.22.0.tar.gz
cd nginx-1.22.0/

对nginx进行配置

./configure --prefix=/usr/local/nginx \
 --user=nginx --group=nginx \
 --with-http_stub_status_module --with-http_ssl_module \
 --with-http_realip_module --with-http_gzip_static_module \
 --with-file-aio --with-http_realip_module 

#查看是否配置成功($? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误)

echo $?
# 成功会显示数字零 
0

编译安装

make -j4
make  -j4 install

查看nginx版本号

/usr/local/nginx/sbin/nginx -v
# 显示结果如下:
nginx version: nginx/1.22.0

检查配置文件语法是否正确

/usr/local/nginx/sbin/nginx -t
# 检查通过显示结果如下:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动nginx

/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload                 # 重新载入配置文件
/usr/local/nginx/sbin/nginx -s stop                   # 快速关闭 Nginx
/usr/local/nginx/sbin/nginx -s quit                   # 关闭Nginx 

编写启动脚本

cd  /usr/lib/systemd/system
vim nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPost=/bin/sleep 0.1
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
ExecStartPre=/usr/bin/rm -f /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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

# 重新加载服务文件
systemctl daemon-reload 
# 开启|停止|重新加载|重启|
systemctl start|stop|reload|restart|status nginx.service

开机自启:

systemctl enable nginx.service

关闭开机自启:

systemctl disable nginx.service

防火墙放行

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

关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

编辑/etc/selinux/config

# SELINUX=enforcing 修改为
SELINUX=disabled
# 或者 执行 
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

保存后执行

setenforce 0

相关内容