Aaron Lau(韩栖)

Nginx 学习笔记 (1) —— 源码安装与开机自启

286 字

测试系统为 CentOS 7.x,在其他 Linux 发行版的操作略有不同。为行文简洁起见,省去非 root 用户的 sudo 赋权命令,下同。

1) 准备好编译环境,安装依赖:PCRE (for regex)、zlib (for gzip) 和 OpenSSL;

yum groupinstall 'Development Tools'
yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel

2) 工作目录可以是任何路径,本系列文依惯例选定 /usr/local/src。源码有 mainline 和 stable 之分,本系列文选择稳定版;

cd /usr/local/src
wget https://nginx.org/download/nginx-1.16.1.tar.gz
tar zxf nginx-1.16.1.tar.gz
cd nginx-1.16.1

3) 编译安装,通常是安装到 /usr/local/nginx。Nginx 的模块一般是静态编译,如果需求有变更,都需要修改参数(可借由 nginx -V 列出现有的)并重编译;

./configure --with-http_ssl_module --with-http_v2_module --with-http_sub_module
make && make install

4) 加入至 systemd 守护进程,实现开机自启,再创建软连接以方便使用。

cd /lib/systemd/system/
wget https://raw.githubusercontent.com/Surfine/Code-Snippets/master/nginx.service
systemctl enable nginx.service
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin

<small>原载 App-scope,有删改。</small>

App-scope · 教程 · Nginx