CentOS 7 源码编译安装 Nginx 1.13.5 完整指南
关于 Nginx 的优势与特性此处不再赘述,既然决定选用其作为 Web 服务器,想必您已具备相应认知,下面直接进入安装环节。
一、环境准备
本教程基于 CentOS 7.3 64位核心版系统。在安装 Nginx 前,需预先配置好相关依赖包,可参考《Centos 7编译安装php7.1之生产篇》中的前置依赖说明,该组件包同样适用于任意版本的 Nginx 安装。
二、新建 web 用户和组
$ /usr/sbin/groupadd www
$ /usr/sbin/useradd -g www www
$ ulimit -SHn 65535 //设置linux高负载参数
三、获取 Nginx 及 OpenSSL 源码
Nginx 提供开发版与稳定版两种选择,生产环境建议采用稳定版(访问 http://nginx.org/en/download.html),以获取最新的 Bug 修复与新特性支持。本文选取当前最新稳定版本 nginx-1.13.5 进行演示。
$ cd /tmp
$ wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz
$ tar zxvf openssl-1.1.0e.tar.gz
$ wget https://nginx.org/download/nginx-1.13.5.tar.gz
$ tar zxvf nginx-1.13.5.tar.gz
$ cd nginx-1.13.5
四、执行 Nginx 编译安装
部分教程中模块指派较少,甚至未指定用户,这通常基于特定需求。为避免后期因功能缺失而重新编译带来的麻烦,建议一次性配置齐全的功能模块。同时,强烈建议指定运行用户与组,这对保障 Nginx 配置的可用性、安全性及稳定性至关重要。
$ ./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-pcre \
--with-openssl=/tmp/openssl-1.1.0e \
--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-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module \
$ make -j8 && make install //编译并安装
五、创建 systemctl 系统 Nginx 单元文件
安装完成后,若希望实现开机自动启动,避免每次重启后手动干预的繁琐操作,需配置系统服务文件。
$ vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP /usr/local/nginx/logs/nginx.pid
ExecStop=/bin/kill -s QUIT /usr/local/nginx/logs/nginx.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
编辑完成后保存并退出编辑器。
六、加入开机自启动并启动 Nginx
$ systemctl enable nginx.service
$ systemctl restart nginx.service
七、配置 Firewalld 防火墙
$ firewall-cmd --zone=public --add-port=80/tcp --permanent
$ firewall-cmd --reload
八、验证 Nginx 启动状态
$ ss -ntlp
通过进程检查可确认 Nginx 已成功运行,至此安装工作全部完成。关于 Nginx 如何解析并支持 PHP 程序的问题,将在后续文章中详细阐述。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。