简介

Nginx 是一款面向性能设计的 HTTP 服务器,能反向代理 HTTP、HTTPS 和邮件相关(SMTP、POP3、IMAP)的协议链接,并且提供了负载均衡以及 HTTP 缓存。它的设计充分使用异步事件模型,削减上下文调度的开销,提高服务器并发能力。采用了模块化设计,提供了丰富的第三方模块。

安装

配置nginx源

1
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装nginx

1
yum install -y nginx

测试并访问IP:80

1
2
[root@web sky]$nginx -v
nginx version: nginx/1.24.0

验证图

如有防火墙,可关闭防火墙或放行端口再重试

yum安装默认适配systemctl服务管理

安装依赖

1
2
3
4
yum install -y pcre-devel
yum -y install gcc make gcc-c++ wget
yum -y install openssl openssl-devel
yum install -y zlib zlib-devel

创建安装目录

1
mkdir -p /usr/local/nginx

进入官网选择对应的版本下载并解压到相应目录,本文以1.24版本为例

1
wget https://nginx.org/download/nginx-1.24.0.tar.gz

解压并进入目录

1
2
3
cd /usr/local/nginx/
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0/

编译安装

1
2
./configure --with-http_ssl_module --prefix=/usr/local/nginx
make && make install

执行./configure可能会报错,报错可以自行安装依赖解决

--prefix可自行设置安装目录,--with-http_ssl_module让nginx支持https功能,非需要可不安装,其他选项自行定制

测试安装情况

1
2
cd /usr/local/nginx/sbin/
./nginx -v

为方便使用,建议添加环境变量,并添加以下内容

1
vim /etc/profile
1
2
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin/
export PATH
1
source /etc/profile

测试全局变量是否生效

1
nginx -v

配置systemctl方式管理

1
vim /usr/lib/systemd/system/nginx.service

加入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

设置开机自启动

1
systemctl enable nginx.service

关闭nginx

1
nginx -s stop

重新加载配置文件并启动

1
2
systemctl daemon-reload
systemctl start nginx

测试服务是否正常

image-20250424084047300

服务管理

常见命令:

  • 启动nginx
1
nginx
  • 重新加载配置
1
nginx -s reload
  • 检查nginx配置是否正常
1
nginx -t
  • 关闭nginx
1
nginx -s stop

基本使用

暂时略,后续补充

参考资料

https://wangchujiang.com/nginx-tutorial/

https://blog.csdn.net/weixin_42506599/article/details/104241721