ubuntu18.04 安装或升级 nginx经验
查看系统版本
命令:lsb_release -a
结果
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.5 LTS
Release: 18.04
Codename: bionic
查看ngnix版本
命令:nginx -V
结果
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-H4cN7P/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
Ubuntu 卸载 nginx 并重新安装
sudo apt-get purge nginx nginx-common # 卸载nginx,包括删除配置文件。
apt install nginx
缺少依赖包解决方法
安装依赖包
方法1:
安装依赖包
apt install gcc g++ unzip bzip2 zlib1g zlib1g-dev libpcre3 libpcre3-dev openssl \
libssl-dev libgeoip1 libgeoip-dev libgeoip-dev make libtool build-essential libgd-dev -y
方法2:
下载源码包解压,自定义下载位置/data/source/
cd /data/source/
wget https://ossjc-1252545319.cos.ap-shanghai.myqcloud.com/other/linux/pcre/pcre-8.44.tar.gz
wget https://ossjc-1252545319.cos.ap-shanghai.myqcloud.com/other/linux/zlib/zlib-1.2.11.tar.gz
wget https://ossjc-1252545319.cos.ap-shanghai.myqcloud.com/other/linux/openssl/openssl-1.1.1f.tar.gz
tar xf pcre-8.44.tar.gz
tar xf zlib-1.2.11.tar.gz
tar xf openssl-1.1.1f.tar.gz
nginx 提示错误:./configure: error: the HTTP XSLT module requires the libxml2/libxslt
解决方法:
sudo apt-get install libxml2 libxml2-dev libxslt-dev
sudo apt-get install libgd2-xpm libgd2-xpm-dev
升级ngnix
方法1:
替换老版本,根据查看ngnix版本内容执行
在/nginx-1.18.0目录下执行
./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-H4cN7P/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
make
备份原有文件,自定义目录/app/backnginx
mv /usr/sbin/nginx /app/backnginx
执行完可以查看版本
cp /djptapp/nginx-1.18.0/objs/nginx /usr/sbin/nginx
检查配置信息
root@app:/app/nginx-1.18.0# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
升级命令 和安装命令
make upgrade
make install
方法2:
官方Nginx存储库在Ubuntu 18.04上安装Nginx最新版本
Nginx维护着Ubuntu的存储库,我们可以使用此存储库来安装最新版本的Nginx,首先,使用以下命令为Nginx创建存储库源文件,Nano是一个命令行文本编辑器(nano命令_Linux nano命令使用详解:字符终端文本编辑器):
sudo nano /etc/apt/sources.list.d/nginx.list
在文件中添加以下两行。
deb [arch=amd64] http://nginx.org/packages/mainline/ubuntu/ bionic nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ bionic nginx
如果你使用Ubuntu 19.04,那么将bionic改为disco:
deb [arch=amd64] http://nginx.org/packages/mainline/ubuntu/ disco nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ disco nginx
要在Nano文本编辑器中保存文件,请按CTRL+O,然后按Enter确认,按CTRL+X退出,为了验证从此存储库下载的包的完整性,我们需要使用以下命令导入Nginx公钥:
wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key
将存储库添加到Ubuntu 18.04系统后,运行以下命令以更新存储库信息:
sudo apt update
如果已从默认的Ubuntu软件存储库安装了Nginx,则需要将其删除:
sudo apt remove nginx nginx-common nginx-full nginx-core
此外,你需要备份主要的Nginx配置文件/etc/nginx/nginx.conf,因为当我们稍后安装最新版本的Nginx时,它将被替换为新的nginx.conf文件:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
你现有的服务器块文件(也称为virtual host文件)将保持不变,现在运行以下命令从nginx.org存储库安装Nginx:
sudo apt install nginx
如果apt包管理器询问你是否要安装新版本的/etc/nginx/nginx.conf文件,则可以回答否
安装Nginx后,测试Nginx配置:
sudo nginx -t
如果测试成功,请启动Nginx:
sudo systemctl start nginx
启动时启用自动启动:
sudo systemctl enable nginx
要检查Nginx的状态,请运行:
systemctl status nginx
输出信息,如下图:
要检查Nginx版本,请使用以下命令:
nginx -v
输出信息:
nginx version: nginx/1.17.0
你还可以查看更多详细信息:
nginx -V
输出信息:
设置Nginx进程用户
来自nginx存储库的Nginx软件包将nginx设置为Nginx进程用户,可以从/etc/nginx/nginx.conf文件的第一行推断出来(如果选择安装新版本的/etc/nginx/nginx.conf文件):
user nginx;
但是,PHP-FPM进程的默认用户和组是www-data,可以在/etc/php/7.2/fpm/pool.d/www.conf文件中看到:
user = www-data
group = www-data
所以我们需要在/etc/nginx/nginx.conf文件中将www-data设置为Nginx进程用户:
sudo nano /etc/nginx/nginx.conf
更改:
user nginx;
为:
user www-data;
保存并关闭文件,然后重新加载Nginx:
sudo systemctl reload nginx
包括服务器块文件
默认情况下,仅包含/etc/nginx/conf.d/目录下的文件,如果你还想在启用站点的目录中使用服务器块文件,请确保在nginx.conf文件的http部分中添加以下行:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
安装Certbot Nginx插件
如果你以前从Ubuntu存储库安装了Nginx,那么当你从nginx存储库安装Nginx时,可能会删除python3-certbot-nginx包,我们需要重新安装它,以便你可以像往常一样自动更新TLS证书:
sudo apt install python3-certbot-nginx