首页 / 大硬盘VPS推荐 / 正文
Nginx 编译安装详细指南,nginx编译安装教程

Time:2025年01月07日 Read:8 评论:42 作者:y21dr45

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也可以作为邮件代理服务器和通用的 TCP/UDP 代理服务器,本文将详细介绍如何在Linux系统上通过编译的方式安装Nginx。

Nginx 编译安装详细指南,nginx编译安装教程

一、准备工作

在进行编译之前,需要确保系统安装了必要的工具和库,以下命令以Debian/Ubuntu为例,其他发行版如CentOS/RHEL请参照相应指令进行调整。

1. 更新软件包列表

sudo apt-get update

2. 安装基础依赖

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g zlib1g-dev libxslt1-dev libgd-dev libgeoip-dev perl

二、下载与解压Nginx源码

访问Nginx官网(http://nginx.org/en/download.html)下载最新版本的Nginx源码包,这里我们以1.25.3版本为例:

wget http://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3

三、配置、编译与安装

1. 配置

运行./configure 脚本进行配置,你可以通过./configure --help 查看详细的配置选项,下面是一个基本的配置示例:

./configure --prefix=/usr/local/nginx \
            --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_auth_request_module \
            --with-http_random_index_module \
            --with-http_secure_link_module \
            --with-http_degradation_module \
            --with-http_slice_module \
            --with-http_stub_status_module \
            --with-http_perl_module \
            --with-http_sub_module \
            --with-pcre \
            --with-pcre-jit \
            --with-stream \
            --with-stream_ssl_module \
            --with-mail \
            --with-mail_ssl_module

2. 编译

配置完成后,执行以下命令进行编译:

make

编译过程可能需要一些时间,具体取决于你的系统性能。

3. 安装

编译成功后,使用以下命令进行安装:

sudo make install

四、启动与配置Nginx

1. 复制配置文件到指定目录

sudo cp -r /usr/local/nginx/conf/* /etc/nginx/
sudo cp -r /usr/local/nginx/html/* /var/www/

2. 修改文件权限

sudo chown -R www-data:www-data /var/www/
sudo chmod -R 755 /var/www/

3. 创建系统服务文件

为了方便管理Nginx服务,可以创建一个systemd服务单元文件:

sudo vi /etc/systemd/system/nginx.service

添加以下内容:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

保存并关闭文件后,重新加载systemd管理器配置:

sudo systemctl daemon-reload

然后启动Nginx服务:

sudo systemctl start nginx

设置Nginx开机自启:

sudo systemctl enable nginx

五、验证安装与配置

1. 检查Nginx服务状态

sudo systemctl status nginx

如果输出信息中显示 "active (running)",则表示Nginx已成功运行。

2. 测试Nginx是否工作正常

在浏览器中访问服务器IP地址或域名,如果能看到Nginx的欢迎页面,说明Nginx正在正常工作。

通过以上步骤,我们从下载源码、配置编译选项、编译安装到最终启动Nginx服务,完整地介绍了如何在Linux系统上编译安装Nginx,编译安装的方式虽然相对复杂,但能够更加灵活地定制Nginx的功能模块,满足特定的业务需求。

标签: nginx编译安装 
排行榜
关于我们
「好主机」服务器测评网专注于为用户提供专业、真实的服务器评测与高性价比推荐。我们通过硬核性能测试、稳定性追踪及用户真实评价,帮助企业和个人用户快速找到最适合的服务器解决方案。无论是云服务器、物理服务器还是企业级服务器,好主机都是您值得信赖的选购指南!
快捷菜单1
服务器测评
VPS测评
VPS测评
服务器资讯
服务器资讯
扫码关注
鲁ICP备2022041413号-1