一、准备相关软件包,并解压
为了实现对系统的最小化影响、改动,这里直接使用Nginx链接OpenSSL、Zlib、PCRE源码包的方式进行编译安装
//下载相应的软件包
wget https://nginx.org/download/nginx-1.20.2.tar.gz
wget https://www.openssl.org/source/openssl-1.1.1n.tar.gz
wget https://zlib.net/zlib-1.2.12.tar.gz
//pcre下载可以到下面网址进行下载
https://sourceforge.net/projects/pcre/files/pcre/8.45/
//下载到的软件包名
nginx-1.20.2.tar.gz
openssl-1.1.1n.tar.gz
pcre-8.45.zip
zlib-1.2.12.tar.gz
//解压软件包
tar -vxzf nginx-1.20.2.tar.gz
tar -vxzf openssl-1.1.1n.tar.gz
tar -vxzf zlib-1.2.12.tar.gz
unzip pcre-8.45.zip
1.1 Nginx源码包结构 Nginx SourceCode Layout
[root@localhost nginx-1.20.2]# ls
auto //包含了很多在执行Configure检查时调用的检测代码
conf //Nginx提供的一些默认配置文件
contrib //贡献的一些有用的脚本
html //Nginx提供的默认的HTTP页面
man //man手册目录
src //Nginx源码目录
CHANGES
CHANGES.ru
configure
LICENSE
README
二、安装软件依赖,编译工具
//CentOS
yum install unzip perl gcc gcc-c++
//Ubuntu
apt install build-essential
三、创建Nginx的运行用户、组,以及目录
groupadd www
useradd -g www -s /sbin/nologin www
mkdir -vp /soft/nginx
四、编译Nginx
根据实际需要可以指定--prefix=PREFIX编译选项(若不指定--prefix选项,默认会将Nginx安装到/usr/local/nginx),以下编译参数为参考
./configure --prefix=/soft/nginx \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-pcre=../pcre-8.45 \
--with-zlib=../zlib-1.2.12 \
--with-openssl=../openssl-1.1.1n \
--with-stream \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module
make && make install
//如果你的机器上有多个CPU核心,可以指定多个make并发编译任务
//例如我这里机器上有4个CPU核心,可以指定-j 4来加快编译速度,但是会让CPU处于高负载
//生产环境慎用
make -j 4 && make install
执行Configure后会在nginx源码目录生成makefile文件和objs目录,以当前编译环境为例,编译安装后的目录结构如下:
[root@localhost]tree /soft/nginx
/soft/nginx
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf //主配置文件
│ ├── nginx.conf.default //以default结尾的是默认配置文件
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs //Nginx服务器还未启动,所有暂时没有日志文件
└── sbin
└── nginx
五、添加Nginx到Systemd管理
//创建一个service文件
nano /usr/lib/systemd/system/nginx.service
//在创建的文件中加入下面的内容,保存
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/soft/nginx/logs/nginx.pid
ExecStartPre=/soft/nginx/sbin/nginx -t
ExecStart=/soft/nginx/sbin/nginx
ExecReload=/soft/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
//修改目录权限
chown -R www:www /soft/nginx
//设置开机启动,并启动Nginx程序
systemctl daemon-reload
service nginx start
systemctl enable nginx.service
六、将Nginx程序文件路径添加到Linux环境变量中(可选)
当我们编译中指定了--prefix编译选项指定目录时,nginx若被指定到非标准目录,只能用绝对路径来运行nginx程序,为了方便在任意目录中运行nginx命令,可将nginx命令目录路径添加到环境变量中
//编辑/etc/profile文件,添加下面的环境变量
//例如我这里Nginx安装路径为/soft/nginx
export NGINX_HOME=/soft/nginx
export PATH=$NGINX_HOME/sbin:$PATH
//使用source命令使得刚刚更改的的Nginx环境变量在当前Shell中立即生效
source /etc/profile
//可以在任意目录中调用nginx命令
root@us4:~# nginx -v
nginx version: nginx/1.20.2