nginx安装与配置

现在网上面关于nginx的文章有很多, 但是按照教程安装完毕后, 后期总有一些问题很棘手, 比如不支持https, 现整理一下单机版安装和配置流程, 避免后期采坑, 系统为CentOS

1.下载nginx

先到nginx官网(http://nginx.org/en/download.html) 查看一下最新版本号, 也可直接下载

wget http://nginx.org/download/nginx-1.19.6.tar.gz

下载完成后解压

tar zxf nginx-1.19.6.tar.gz    

2.安装

cd到解压nginx目录, 下面开始编译, 这个步骤非常重要!

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-stream

解释一下, configure后面的参数属于nginx的模块, 比如--with-http_ssl_module就代表nginx支持https请求, 其他的可以自行搜索, 这里基本上把后面能用到的都有了, nginx的所有模块必须在编译的时候添加,不能在运行的时候动态加载, 如果后面需要增加其他模块, 只能重新编译安装

编译完成后开始安装

make & make install

至此安装就结束了

3.启动

nginx -t
# 重启
nginx -s reload

4.注册服务

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
vi /lib/systemd/system/nginx.service

输入

[Unit]
Description=nginx
After=network.target

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

[Install]
WantedBy=multi-user.target


保存, systemctl enable nginx.service 即可

举报
评论 0