编译安装Nginx
因为Ubuntu 18.04 LTS使用apt-get install nginx安装的nginx是不支持tls1.3的,所有需要从源码编译安装
本文使用root用户,在/root/目录下操作
需要的源码
openssl-1.1.1c
pcre-8.43
cloudflare zlib
nginx-1.17.3
安装编译工具
apt-get install git gcc make build-essential -y
下载源码
新建一个src目录,用来放下载的源码
mkdir src && cd src
wget --no-check-certificate https://nginx.org/download/nginx-1.17.3.tar.gz
tar -zxvf nginx-1.17.3.tar.gz && rm nginx-1.17.3.tar.gz && mv nginx-1.17.3/ nginx
wget --no-check-certificate https://www.openssl.org/source/openssl-1.1.1c.tar.gz
tar -zxvf openssl-1.1.1c.tar.gz && rm openssl-1.1.1c.tar.gz && mv openssl-1.1.1c/ openssl
# Don't choose pcre2
wget --no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar -zxvf pcre-8.43.tar.gz && rm pcre-8.43.tar.gz && mv pcre-8.43/ pcre
git clone https://github.com/cloudflare/zlib.git
# If you get the error below,you need run 'apt-get install --reinstall ca-certificates'
# fatal: unable to access 'https://github.com/cloudflare/zlib.git': Problem with the SSL CA cert (path? access rights?)
cd zlib/
make -f Makefile.in distclean
cd
其他编译配置
在编译 nginx 时,默认会以 debug 模式来运行,取消 debug 的好处是编译后的 nginx 更加小巧,编译更快。
vim /root/src/nginx/auto/cc/gcc
## 注释掉下边的两行
-------------------------
# debug
CFLAGS="$CFLAGS -g"
-------------------------
编译安装
cd ~/src/nginx
./configure \
# 设置使用时的默认用户
--user=www-data \
# 设使用时的工作组
--group=www-data \
# 编译生成文件目录
--prefix=/usr/local/nginx \
# 设置可执行文件的名称,此名称仅在安装期间使用
--sbin-path=/usr/local/nginx/sbin/nginx \
# 设置配置文件路径
--conf-path=/usr/local/nginx/conf/nginx.conf \
# 设置将存储主进程的进程标识的文件的名称
--pid-path=/run/nginx.pid \
# 设置日志路径,需提前创建/var/log/nginx目录
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
# Optional Modules
--with-compat \
--with-file-aio \
--with-threads \
--with-http_v2_module \
--with-http_realip_module \
--with-openssl=../openssl --with-http_ssl_module \
--with-pcre=../pcre --with-pcre-jit \
--with-zlib=../zlib --with-http_gzip_static_module \
make && make install
配置conf
编译安装的 nginx,目录结构和 apt 安装的有大同小异。apt 安装时, nginx.conf 配置文件会在安装目录的根目录下。编译安装时, nginx.conf 配置文件会在安装目录的 conf 文件夹下
这里位于/etc/nginx/conf/nginx.conf,记得在里面添加pid /run/nginx.pid;,默认是注释掉的
为nginx配置systemd
vim /etc/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target
[Service]
Type=forking
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
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 start nginx #运行
我们现在可以用service或systemctl管理nginx了
service nginx restart
查看版本
/usr/local/nginx/sbin/nginx -V
参考资料:
PID file /run/nginx.pid not readable (yet?) after start