Linux源码编译安装Nginx

6/20/2023 Linux

Nginx是目前最火的web服务器,可以做端口转发,负载均衡,邮件代理服务器等,功能十分强大;下面详解如何在Linux源码方式安装Nginx。

# 下载安装包

Nginx官方下载地址:http://nginx.org/en/download.html (opens new window),有3种版本可选:Mainline version(主线版)、Stable version(稳定版)、Legacy versions(旧版); 选择稳定版1.24,将源码包直接下载到服务器src目录

cd /usr/local/src
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -xzvf nginx-1.24.0.tar.gz

# 编译安装

安装编译工具

yum -y install make zlib zlib-devel gcc-c++ pcre pcre-devel libtool openssl openssl-devel 

编译安装

cd /usr/local/src/nginx-1.24.0
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
make && make install
1
2
3

参数说明:

  • --prefix=/usr/local/webserver/nginx 指定安装目录
  • --conf-path=/usr/local/webserver/nginx 设置nginx.conf配置文件的位置
  • --pid-path=/usr/local/webserver/nginx 设置nginx.pid的位置
  • --with-http_stub_status_module --with-http_ssl_module 开启https,添加ssl模块

配置环境变量

echo "export PATH=/usr/local/webserver/nginx/sbin/:$PATH" >> /etc/profile
source /etc/profile

查看版本号,测试是否安装成功

[root@jason ~]# nginx -v
nginx version: nginx/1.24.0
1
2

最后再用浏览器访问80端口,看是否成功

[root@jason ~]# curl -i localhost:80
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Tue, 20 Jun 2023 08:47:07 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 20 Jun 2023 08:33:59 GMT
Connection: keep-alive
ETag: "64916477-267"
Accept-Ranges: bytes

<!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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

返回http200,和html源代码说明已经安装成功,到此Nginx安装完成。

# Nginx常用命令

nginx -t #语法检查
nginx -s stop #停止Nginx
nginx -s quit #停止Nginx
nginx -s reload #win重启nginx
nginx -s reopen #linux重启Nginx
1
2
3
4
5
Last Updated: 7/5/2023, 4:03:58 PM