Nginx 官方源码部署通常涉及以下几个步骤:
下载 Nginx 源码
首先,你需要从 Nginx 官方网站下载最新版本的源码包。访问 Nginx官网 下载页面,选择适合您操作系统的稳定版或者主线版源码包。
wget http://nginx.org/download/nginx-x.y.z.tar.gz # 替换 x.y.z 为实际版本号
tar zxf nginx-x.y.z.tar.gz
cd nginx-x.y.z
安装依赖包
在安装 Nginx 源码之前,确保系统已经安装了必要的构建工具和依赖包,例如在 CentOS 或 RHEL 系统中:
sudo yum install -y gcc make automake autoconf libtool zlib-devel pcre-devel openssl-devel
在 Debian 或 Ubuntu 系统中:
sudo apt-get update
sudo apt-get install -y build-essential libpcre3-dev zlib1g-dev libssl-dev
配置与编译
接下来,配置 Nginx 源码以适应您的系统环境,并决定是否启用额外模块:
./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/local/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--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-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-threads \
--with-stream \
--with-stream_ssl_module
这里的配置选项示例包含了常见的HTTP模块以及邮件模块的支持,并设置了安装路径。您可以根据实际需求定制这些配置选项。
编译并安装
执行 make
来编译源码:
make
然后,使用 make install
将编译好的 Nginx 安装到指定位置:
sudo make install
启动 Nginx
安装完成后,您可以使用如下命令启动 Nginx:
sudo /usr/local/nginx/sbin/nginx
为了使 Nginx 在系统启动时自动运行,还需要添加相应的启动脚本和服务单元文件,具体取决于您的系统类型(System V init 或 systemd 等)。
配置防火墙
如果使用了防火墙(如iptables或firewalld),别忘了开放 HTTP(80)和 HTTPS(443)端口:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
测试安装
最后,通过访问服务器IP地址确认Nginx是否正常工作,并检查 /usr/local/nginx/html
目录下的默认欢迎页。
以上步骤适用于一般的 Linux 环境下通过源码编译安装 Nginx,具体细节可能因操作系统版本不同略有差异,请根据实际情况调整。