让我们开始编译并安装nginx,并确保不改变文章大意和图片位置。
下载Nginx安装文件
首先,我们需要下载Nginx的安装包。以下是相关的命令:
# 切换到/opt目录 root@master:~# cd /opt <h1>新建文件夹</h1><p>root@master:/opt# mkdir nginx</p><h1>下载Nginx安装包</h1><p>root@master:/opt/nginx# wget -c <a href="https://www.php.cn/link/5e1fe0a89f56e5e59e057b49aae118bb">https://www.php.cn/link/5e1fe0a89f56e5e59e057b49aae118bb</a></p><h1>解压安装包</h1><p>root@master:/opt/nginx# tar -zxvf nginx-1.24.0.tar.gz
安装Nginx依赖库
接下来,我们需要安装Nginx所需的依赖库。以下是安装命令:
apt-get install -y gcc apt-get install -y libpcre3 libpcre3-dev apt-get install -y zlib1g zlib1g-dev apt-get install -y openssl apt-get install -y libssl-dev apt-get install -y make
编译并安装Nginx
现在我们可以开始编译并安装Nginx了。请注意,您可以指定配置文件目录和自定义安装模块:
# 进入Nginx安装文件内部 root@master:/opt/nginx# cd nginx-1.24.0</p><h1>执行编译</h1><p>root@master:/opt/nginx/nginx-1.24.0# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module && make && make install
配置Nginx环境变量
编译安装命令执行完成后,我们可能无法直接使用nginx命令,因为系统无法识别它。接下来我们需要配置Nginx的环境变量。

配置Nginx环境变量:
# 编辑/etc/profile文件,在最后追加一行 export PATH=/usr/local/nginx/sbin:$PATH</p><h1>保存修改并使配置文件生效</h1><p>source /etc/profile
再次查看Nginx版本信息:

启动Nginx
尝试启动Nginx时,您可能会发现缺少Nginx服务。

此时,我们需要添加Nginx服务:
# vi /lib/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</p><p>[Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true</p><p>[Install] WantedBy=multi-user.target
新建nginx.pid文件
我们需要在/var/run目录下创建nginx.pid文件,并修改nginx.conf配置文件:
# 在/var/run目录下新建文件nginx.pid root@master:/var/run# touch nginx.pid</p><h1>修改nginx.conf配置文件</h1><p>root@master:~# vi /etc/nginx/nginx.conf</p><h1>将"#pid logs/nginx.pid;"改为pid /var/run/nginx.pid;</h1><h1>保存并退出:wq
然后就可以启动Nginx了。
Nginx常用命令
以下是Nginx的一些常用命令:
# 启动Nginx</h1><p>service nginx start</p><h1>重启Nginx</h1><p>service nginx restart</p><h1>重新加载</h1><p>nginx -s reload</p><h1>停止Nginx</h1><p>service nginx stop
通过以上步骤,您可以成功编译并安装Nginx,并配置好其环境变量和服务。
以上就是Nginx编译安装(Ubuntu 20.04.6 LTS)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号