查看当前系统环境
lsb_release -a

当前系统环境
更新apt源
apt update
1. 安装gcc,g++开发类库
apt install build-essential
apt install libtool
2. 安装pcre库
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。
nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在linux 上安装pcre库
apt install libpcre3 libpcre3-dev
3. 安装zlib库
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用zlib 对 http 包的内容进行 gzip
apt install zlib1g-dev
4. 安装oepnssl库
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要安装 OpenSSL 库
apt install openssl libssl-dev
5. 安装nginx
- 切换目录
cd /usr/local
- 下载
nginx
我选择的版本是nginx-1.19.0, 可以自行修改对应版本
wget http://nginx.org/download/nginx-1.19.0.tar.gz
- 解压文件
tar -zxvf nginx-1.19.0.tar.gz
- 切换目录
cd nginx-1.19.0
- 预编译
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
- 安装
make && make install
- 创建启动软连接
ln -s /usr/local/nginx/sbin/nginx /bin/nginx
6. 部署成系统服务
- 编辑文件
vim lib/systemd/system/nginx.service
- 输入以下内容并保存
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
- 重新加载配置文件
systemctl daemon-reload
- 开机启动服务
systemctl enable nginx
- 启动服务
systemctl restart nginx
7. 卸载nginx相关服务
用于安装更新版本的nginx,记得提前备份文件
- 卸载删除除了配置文件以外的所有相关文件。
apt remove nginx nginx-common
- 卸载删除所有相关文件。
apt purge nginx nginx-common
apt remove nginx-full nginx-common
