自学内容网 自学内容网

Ubuntu22.04离线安装nginx

下载安装包

  • nginx
    nginx下载地址,选stable的即可,传到服务器上面,记住上传路径
    提示: 下面的openssl,zlib,pcre也可以不下载也可以,我这里是考虑到完全离线下载的情况
  • openssl
    这个是https需要弄得,如果生产环境不是https那可以不用装
    openssl
    在这里插入图片描述

这个下载需要科学上网

  • zlib
    zlib
    这个是支持gzip压缩的模块
    在这里插入图片描述
  • pcre

这个是正则表达式模块

pcre链接
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

我待会放一个网盘链接,下载一下就行百度网盘链接

上传到服务器当中,记住路径,我这里就上传到/usr/software/

安装gcc相关

apt install gcc build-essential make

编译nginx

进入nginx安装目录,应该是如图片所示
在这里插入图片描述
如果你没有下载openssl,pcre,zlib的话,执行下面的

./configure --prefix=/usr/software/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module # prefix后面跟的是nginx的安装目录,这里的意思就是说要把nginx安装到/usr/software/nginx下面
sudo make && make install

下载了pcre,openssl,zlib就执行下面的即可

./configure --prefix=/usr/software/nginx --with-http_ssl_module --with-openssl=../openssl --with-pcre=../pcre --with-zlib=../zlib # --prefix是指定安装目录的,即把nginx安装到/usr/software/nginx当中
sudo make && make install

这个过程要等一会儿,我等了4分钟

cd nginx
cd sbin
./nginx 
# 打开浏览器,在地址栏输入localhost,看到一个欢迎页面就行
# 若是命令行的话执行下面的语句
curl localhost

curl localhost的结果是

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

就行了

交给systemd托管

vim /usr/lib/systemd/system/nginx.service
输入以下内容(内容中的路径需要改成你自己的)

[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/software/nginx/logs/nginx.pid
ExecStartPre=/usr/software/nginx/sbin/nginx -t -c /usr/software/nginx/conf/nginx.conf
ExecStart=/usr/software/nginx/sbin/nginx -c /usr/software/nginx/conf/nginx.conf
ExecReload=/usr/software/nginx/sbin/nginx -s reload
ExecStop=/usr/software/nginx/sbin/nginx -s stop
ExecQuit=/usr/software/nginx/sbin/nginx -s quit # 
PrivateTmp=true
[Install]
wantedBy=multi-user.target
# 将服务文件复制到 /etc/systemd/system/ 目录(如果它不在那里的话)
sudo cp /usr/lib/systemd/system/nginx.service /etc/systemd/system/nginx.service

# 重新加载 systemd 以识别新的服务文件
sudo systemctl daemon-reload

# 启动 Nginx 服务
sudo systemctl start nginx

# 检查 Nginx 服务的状态
sudo systemctl status nginx

# 如果您希望 Nginx 服务在系统启动时自动运行,可以设置它为开机自启
sudo systemctl enable nginx

报错

cp: conf/koi-win‘ and /usr/local/nginx/conf/koi-win‘ are the same file

解决办法,nginx解压后会有一个nginx-x.x.x的文件夹,这个文件夹不要删除,在nginx-x.x.x同级的文件夹中新建一个nginx文件夹即可
比如:nginx-x.x.x的路径是/usr/software/nginx-x.x.x,那么nginx文件夹的路径就是/usr/software/nginx,就行

7月 23 11:44:48 notdy nginx[101959]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

98号端口占用了

ps -ef | grep nginx 

在这里插入图片描述
不用记住最后一行的pid

sudo kill -9 101341 # 这个是上面图片的第一行的PID
sudo kill -9 101342 # 这个是上面图片的第一行的PID

原文地址:https://blog.csdn.net/qq_45418837/article/details/140621278

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!