一.Nginx的安装
1.安装编译 NGINX 所需的依赖并下载NGINX 源码包
在开始编译之前,需要安装一些基本的依赖和工具。使用以下命令安装这些依赖:
sudo yum install -y gcc make pcre pcre-devel zlib zlib-devel openssl openssl-devel
从 NGINX 官方网站(https://nginx.org/en/download.html)下载最新的稳定版 NGINX 源码包,并上传到 CentOS 服务器上。
本次教程安装的为当前最新版:Nginx-1.25.3
wget https://nginx.org/download/nginx-1.25.3.tar.gz
2.解压并进入目录进行编译安装
#解压下载的nginx压缩包
tar -xzvf nginx-1.25.3.tar.gz
#进入解压好的nginx目录准备进行编译
cd nginx-1.25.3/
进入解压后的目录。然后执行如下命令配置编译选项:
./configure --prefix=/usr/share/nginx --group=nginx --user=nginx --sbin-path=/usr/share/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/usr/share/nginx/log/error.log --http-log-path=/usr/share/nginx/log/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
以下是对每个参数的解释:
--prefix=/usr/share/nginx
:指定Nginx的安装路径为/usr/share/nginx
。--group=nginx
:指定Nginx运行的组为nginx
。--user=nginx
:指定Nginx运行的用户为nginx
。--sbin-path=/usr/share/nginx/sbin/nginx
:指定Nginx可执行文件的路径为/usr/share/nginx/sbin/nginx
。--conf-path=/etc/nginx/nginx.conf
:指定Nginx的配置文件路径为/etc/nginx/nginx.conf
。--error-log-path=/usr/share/nginx/log/error.log
:指定Nginx的错误日志路径为/usr/share/nginx/log/error.log
。--http-log-path=/usr/share/nginx/log/access.log
:指定Nginx的访问日志路径为/usr/share/nginx/log/access.log
。--pid-path=/var/run/nginx.pid
:指定Nginx的进程ID文件路径为/var/run/nginx.pid
。--lock-path=/var/lock/nginx
:指定Nginx的锁文件路径为/var/lock/nginx
。--with-http_stub_status_module
:启用Nginx的stub_status模块,用于获取Nginx的状态信息。--with-http_ssl_module
:启用Nginx的SSL模块,支持HTTPS协议。--with-http_gzip_static_module
:启用Nginx的gzip_static模块,用于压缩静态文件。--with-pcre
:启用PCRE库,用于支持正则表达式。--with-http_realip_module
:启用Nginx的realip模块,用于获取真实客户端IP地址。--with-stream
:启用Nginx的stream模块,用于处理TCP和UDP流量。
出现如图则可以执行编译安装了:
编译安装:
make install
3.启动Nginx
创建NGINX 存储的临时文件:
mkdir -p /tmp/nginx/client_body
mkdir -p /tmp/nginx/proxy
mkdir -p /tmp/nginx/fastcgi
启动Nginx进行测试:
设置软连接,用nginx直接启动:
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
#启动Nginx服务:
nginx
#检查Nginx端口是否起来:
ss -antup |grep 80
udp UNCONN 0 0 [fe80::a969:b1dc:8c4b:55c2]%ens192:546 [::]:* users:(("dhclient",pid=1442,fd=5))
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=3737,fd=6),("nginx",pid=3736,fd=6))
#通过curl命令访问本地网站看是否有nginx网页信息:
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>
4.设置Nginx的service服务
为了使用 systemctl 命令运行 Nginx,需要创建一个 service 文件。以下是一个示例 service 文件,将其保存为/usr/lib/systemd/system/nginx.service并赋予可执行的权限.
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/share/nginx/sbin/nginx
ExecReload=/usr/share/nginx/sbin/nginx -s reload
ExecStop=/usr/share/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod +x /usr/lib/systemd/system/nginx.service
请根据您的实际情况进行相应的修改Nginx 安装路径。
重新加载系统和服务配置文件:
systemctl daemon-reload
通过systemctl启动Nginx与检查Nginx运行状态
[root@mancxi nginx-1.25.3]# systemctl start nginx
[root@mancxi nginx-1.25.3]# systemctl status nginx
● nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2023-11-08 21:42:40 EST; 3min 8s ago
Process: 29580 ExecStart=/usr/share/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 29581 (nginx)
CGroup: /system.slice/nginx.service
├─29581 nginx: master process /usr/share/nginx/sbin/nginx
└─29582 nginx: worker process
Nov 08 21:42:40 centos-dzz systemd[1]: Starting nginx...
Nov 08 21:42:40 centos-dzz systemd[1]: Started nginx.
按照上门的检查Nginx的端口以及访问nginx的命令是否可以访问Nginx既可。