在CentOS 8上自定义安装Nginx到非默认目录需要从源代码编译安装。以下是详细步骤:
sudo dnf install -y gcc make pcre-devel zlib-devel openssl-devel wget
假设你想将Nginx安装到/opt/nginx
目录:
sudo mkdir -p /opt/nginx
sudo chown $(whoami):$(whoami) /opt/nginx
cd /tmp
wget http://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
./configure \
--prefix=/opt/nginx \
--sbin-path=/opt/nginx/sbin/nginx \
--conf-path=/opt/nginx/conf/nginx.conf \
--pid-path=/opt/nginx/logs/nginx.pid \
--error-log-path=/opt/nginx/logs/error.log \
--http-log-path=/opt/nginx/logs/access.log \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-threads
make
sudo make install
创建/etc/systemd/system/nginx.service
文件:
sudo nano /etc/systemd/system/nginx.service
内容如下:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx
/opt/nginx/sbin/nginx -v
sudo systemctl status nginx
curl http://localhost
为了方便使用,可以将Nginx可执行文件路径添加到PATH:
echo 'export PATH=$PATH:/opt/nginx/sbin' >> ~/.bashrc
source ~/.bashrc
/opt/nginx/conf/nginx.conf
/opt/nginx/logs/
目录--with-module_name
指定这样你就成功在CentOS 8上自定义目录安装了Nginx。