Nginx系列-Linux下安装Nginx(一)
原始的文章记录已经找不到了,现在使用 Rocky Linux 9.3 进行替换。 更新时间:2024 年 5 月 5 日
安装 Nginx 的方法有很多种,本篇文章只讲源码安装。
1)更新系统
dnf -y update
2)安装依赖包
dnf -y install wget vim tar
dnf -y install gcc gcc-c++
dnf -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
3)创建 nginx 用户与组
groupadd nginx
useradd -g nginx nginx -M -s /sbin/nologin
4)下载源码并解压
cd /usr/local/src/
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
5)预编译与安装
./configure \
--prefix=/usr/local/software/nginx-1.24.0 \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-stream_ssl_module
# 编译与安装
make && make install
6)配置开机自启
vim /usr/lib/systemd/system/nginx.service
添加如下内容
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/software/nginx-1.24.0/sbin/nginx
ExecReload=/usr/local/software/nginx-1.24.0/sbin/nginx -s reload
ExecStop=/usr/local/software/nginx-1.24.0/sbin/nginx -s quit
[Install]
WantedBy=multi-user.target
然后保存退出
7)设置开机自启
systemctl enable nginx.service
# 启动 nginx
systemctl start nginx.service
其他命令
# 留意留意如下命令
# 禁止开机自启
systemctl disable nginx.service
# 查看nginx状态
systemctl status nginx.service
8)重看 nginx 状态
systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
Active: active (running) since Fri 2024-01-26 23:15:08 CST; 17min ago
Process: 51087 ExecStart=/usr/local/software/nginx-1.24.0/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 51088 (nginx)
Tasks: 2 (limit: 10822)
Memory: 1.9M
CPU: 7ms
CGroup: /system.slice/nginx.service
├─51088 "nginx: master process /usr/local/software/nginx-1.24.0/sbin/nginx"
└─51089 "nginx: worker process"
Jan 26 23:15:08 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
Jan 26 23:15:08 localhost.localdomain systemd[1]: Started nginx - high performance web server.
9)浏览器访问
浏览器中访问 http://192.168.73.129/
,看到 Welcome to nginx!
页面即可。
请登录后再评论