安装相关依赖并下载源码

1
2
sudo apt-get install libpcre3 libpcre3-dev openssl libssl-dev build-essential git tree zlib1g-dev
wget http://nginx.org/download/nginx-1.20.2.tar.gz

nginx下载列表及不同版本下载链接:http://nginx.org/en/download.html

编译安装

1
2
3
4
5
6
7
8
tar -zxvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
./configure --with-http_ssl_module
make
sudo make install
echo 'PATH=$PATH:/usr/local/nginx/sbin/'>>/etc/profile
source /etc/profile
nginx -v

设置开机启动

1
vim /lib/systemd/system/nginx.service

文件内容为

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

然后运行以下指令

1
2
sudo systemctl enable nginx
sudo systemctl start nginx

默认配置文件路径:/usr/local/nginx/conf/nginx.conf 默认安装路径:usr/local/nginx