Ubuntu22.04离线安装nginx

下载安装包

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

这个下载需要科学上网

  • zlib
    zlib
    这个是支持gzip压缩的模块
  • pcre

这个是正则表达式模块

pcre链接


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

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

安装gcc相关

shell 复制代码
apt install gcc build-essential make

编译nginx

进入nginx安装目录,应该是如图片所示

如果你没有下载openssl,pcre,zlib的话,执行下面的

shell 复制代码
./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就执行下面的即可

shell 复制代码
./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分钟

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

curl localhost的结果是

html 复制代码
<!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

输入以下内容(内容中的路径需要改成你自己的)

shell 复制代码
[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
shell 复制代码
# 将服务文件复制到 /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号端口占用了

shell 复制代码
ps -ef | grep nginx 

不用记住最后一行的pid

shell 复制代码
sudo kill -9 101341 # 这个是上面图片的第一行的PID
sudo kill -9 101342 # 这个是上面图片的第一行的PID
相关推荐
惊讶的猫19 分钟前
Redis持久化介绍
数据库·redis·缓存
Apple_羊先森31 分钟前
ORACLE数据库巡检SQL脚本--19、磁盘读次数最高的前5条SQL语句
数据库·sql·oracle
全栈前端老曹1 小时前
【MongoDB】Node.js 集成 —— Mongoose ORM、Schema 设计、Model 操作
前端·javascript·数据库·mongodb·node.js·nosql·全栈
神梦流1 小时前
ops-math 算子库的扩展能力:高精度与复数运算的硬件映射策略
服务器·数据库
让学习成为一种生活方式2 小时前
trf v4.09.1 安装与使用--生信工具42-version2
数据库
啦啦啦_99992 小时前
Redis-5-doFormatAsync()方法
数据库·redis·c#
生产队队长2 小时前
Redis:Windows环境安装Redis,并将 Redis 进程注册为服务
数据库·redis·缓存
老邓计算机毕设2 小时前
SSM找学互助系统52568(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架·javaweb 毕业设计
痴儿哈哈2 小时前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python
Σίσυφος19002 小时前
PCL法向量估计 之 方向约束法向量(Orientation Guided Normal)
数据库