Centos7编译安装Nginx1.20

Centos7编译安装Nginx1.20

05050

首先,前往nginx: download下载源码

或者,直接使用wget下载并使用tar解压即可

wget http://nginx.org/download/nginx-1.20.0.tar.gz
tar -xvzf nginx-1.20.0.tar.gz 

这次我想额外添加一下Brotli模块,所以再下载一下Brotli

git clone https://github.com/google/ngx_brotli
 cd /home/ngx_brotli && git submodule update --init && cd /home/nginx-1.20.0

移动到nginx文件目录,开始编译

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-http_realip_module --with-http_gzip_static_module  --add-module=/home/ngx_brotli 
make && make install

编译完成后,可以添加一个软连接方便直接管理nginx

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

现在,执行nginx即可启动

添加nginx到systemctl守护

进入/usr/lib/systemd/system,新建编辑文件nginx.service

cd  /usr/lib/systemd/system
vi nginx.service

按下列样式编辑

[Unit]
Description=nginx - high performance web server 
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

然后执行下列命令启动/管理Nginx

systemctl daemon-reload
//重新加载 systemd
systemctl enable nginx
//开机启动nginx
systemctl start nginx
//启动nginx
systemctl restart nginx
//重启nginx
systemctl reload nginx
//重载nginx配置文件

一些碰到的问题:

1../configure: error: the HTTP rewrite module requires the PCRE library.

执行下列命令安装pcre-devel openssl openssl-devel模块

yum -y install pcre-devel openssl openssl-devel

2./configure: error: the HTTP cache module requires md5 functions from OpenSSL library.  

执行问题1的命令即可

3../configure: error: Brotli library is missing from the /usr directory.

执行错误信息下面的提示即可,例如下图

图片[1]-Centos7编译安装Nginx1.20-Rain's Blog

4.nginx: [emerg] getpwnam(“www”) failed

没有添加www用户/用户组,执行下列命令

/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www

4.ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=1/FAILURE)

将Nginx直接kill掉,重新拉起即可

图片[2]-Centos7编译安装Nginx1.20-Rain's Blog

参考资料

© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏
分享
评论 抢沙发
Rain的头像-Rain's Blog

昵称

取消
昵称表情代码图片