如何编译安装nginx?

Nginx

1 基础知识

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器
Nginx 同时也提供了IMAP/POP3/SMTP服务
Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的
Nginx第一个公开版本0.1.0发布于2004年10月4日。
Nginx源代码以类BSD许可证的形式发布
Nginx因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名

2 最佳实践

2.1 系统环境

2.1.1 系统信息

OS = CentOS 7.x x86_64
IP Addresses = 10.168.0.80
Host Name = www.cmdschool.org

2.1.2 配置防火墙

firewall-cmd --permanent --add-service http --add-service https
firewall-cmd --reload
firewall-cmd --list-all

2.1.3 关闭selinux

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

2.1.4 升级系统

yum update -y

2.2 准备工作

2.2.1 准备编译环境

yum install -y gcc gcc-c++ make expat-devel autoconf

2.2.2 备份当前配置并删除rpm安装包

cd ~
tar -cvzf nginx-conf.tar.bz /etc/nginx/
yum remove -y nginx

2.2.3 下载并解压安装包

cd ~/
wget https://nginx.org/download/nginx-1.22.1.tar.gz
tar -xf nginx-1.22.1.tar.gz

2.2.4 创建运行用户

groupadd  -g 988 nginx
useradd -u 990 -g 988 -d /var/cache/nginx -s /bin/bash nginx -c "nginx user"

2.3 编译安装

2.3.1 预编译软件包

cd ~/nginx-1.22.1/
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

另外,如果你需要参考已经编译的Nginx编译参数,建议你可以使用如下命令,

nginx -V

如果遇到如下提示,

./configure: error: the HTTP rewrite module requires the PCRE library.

请使用如下命令解决依赖关系,

yum install -y pcre-devel

如果遇到如下提示,

./configure: error: SSL modules require the OpenSSL library.

请使用如下命令解决依赖关系,

yum install -y openssl-devel

2.3.2 编译软件包

cd ~/nginx-1.22.1/
make

2.3.3 安装软件包

cd ~/nginx-1.22.1/
make install

2.3.4 验证安装

nginx -v

可见如下输出,

nginx version: nginx/1.22.1

2.4 配置软件

2.4.1 创建启动脚本

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP \$(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM \$(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target
EOF

脚本创建后,你需要使用如下命令重载并使服务生效,

systemctl daemon-reload

2.4.2 启动服务并设置自启动

systemctl start nginx.service
systemctl enable nginx.service

另外,你可以使用如下命令查询服务状态,

systemctl status nginx.service

参阅文档
============================
安装包下载
————–
https://github.com/nginx/nginx/tags
https://nginx.org/download/

安装文档
————–
https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/

没有评论

发表回复

Nginx
如何解决https请求不安全http页被阻止问题?

1 前言 一个问题,一篇文章,一出故事。 笔者最近代理公司应用,发现https的页面有请求不安全的h …

Nginx
如何Nginx代理上游的子项目或文件夹?

1 前言 一个问题,一篇文章,一出故事。 笔者接到任务需要把Tomcat的其中一个应用号使用Ngin …

Nginx
如何安装部署RHEL 9 Nignx?

1 前言 一个问题,一篇文章,一出故事。 笔者需要基于RHEL 9部署Nginx环境,于是整理此文。 …