nginx源码编译安装

本文档详细介绍了如何在CentOS7上编译安装Nginx 1.16.1,包括安装依赖包、下载软件、配置编译选项、安装、设置环境变量、创建nginx用户、配置service文件以及启动和检查Nginx服务。同时提供了自动安装的bash脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以CentOS7作为举例,安装Nginx1.16.1
1:安装依赖包
[root@centos7-1 ~]# yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
以上安装包并非全部都需要 只不过是CentOS7初始化比较常用的依赖包,都建议安装

gcc为GNU Compiler Collection的缩写,可以编译c和c++源代码等,它是GNU开发的C和C++以及其他很多种语言的编译器
gcc在编译C++源代码的阶段,只能编译c++源文件,而不能自动和c++程序使用的库链接(编译过程分为编译,链接两个阶段,注意不要和可执行文件这个概念搞混淆,相对可执行文件来说有三个重要的概念:编译(compile),链接(link),加载(load)。源程序文件被编译成目标文件,多个目标文件连接库被链接成一个最终的可执行,可执行文件被加载到内存中运行)。因此,通常使用g++命令来完成C++程序的编译和连接,该程序会自动调用gcc实现编译
gcc-c++也能编译c源代码,只不过会把它当成c++源代码,后缀为.c的,gcc把它当作c程序,而g++当作是c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的
automake是一个从Makefile.am文件自动生成Makefile.in的工具。为了生成Makefile.in,automake还需用到perl,由于automake创建的发布完成遵循GNU标准,所以在创建中不需要perl。libtool是一款方便生成各种程序库的工具。
pcre pcre-devel:在Nginx编译需要PCRE(Perl Compatible Reqular Expression),因为Nginx 的Rewrite模块和HTTP核心模块会使用到PCRE正则表达式语法。
zlip zlib-devel:nginx启用压缩功能的时候,需要此模块的支持
openssl openssl-devel:开启ssl的时候需要此模块的支持。

2:下载Nginx软件包,下载地址
wget https://nginx.org/download/nginx-1.16.1.tar.gz
3:将软件包移至指定目录并解压,这里放到的是/data/download
[root@centos7-1 download]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@centos7-1 download]# ll
total 1012
-rw-r–r-- 1 root root 1032630 Aug 14 2019 nginx-1.16.1.tar.gz
[root@centos7-1 download]# pwd
/data/download
4:解压nginx
[root@centos7-1 download]# tar xfv nginx-1.16.1.tar.gz

5:编译nginx软件
./configure --prefix=/data/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
这里参数含义是
–prefix 表示安装目录
–user --group 表示运行nginx的所属组和属主
–with-http_ssl_module 允许构建一个模块,将HTTPS 协议支持添加 到 HTTP 服务器
–with-http_v2_module 允许构建一个支持HTTP/2的模块
–with-http_realip_module 启用构建ngx_http_realip_module 模块,该 模块将客户端地址更改为在指定标头字段中发送的地址。也就是可以自定义http报文字段来表示客户端IP,在负载均衡里面会使用
–with-http_stub_status_module 启用构建ngx_http_stub_status_module 模块,该 模块提供对基本状态信息的访问。
–with-http_gzip_static_module 启用构建ngx_http_gzip_static_module 模块,该 模块可以发送带有“ .gz”文件扩展名的预压缩文件,而不是常规文件。支持压缩。主要压缩的还是txt类的文本
–with-pcre 强制使用 PCRE 库。Nginx服务器利用ngx_http_rewrite_module模块解析和处理rewrite请求,此功能依靠PCRE(perl compatible regularex pression),因此编译之前要安装PCRE库
–with-stream_ssl_module 允许构建一个模块,将 SSL/TLS 协议支持添加 到流模块。
–with-stream_realip_module 启用构建ngx_stream_realip_module 模块,该 模块将客户端地址更改为在 PROXY 协议标头中发送的地址。例如php代理的时候。

6:安装nginx 命令如下
make && make install

7:设置环境变量,否则无法在全局使用nginx命令,例如nginx -s reload
echo “PATH=/data/usr/local/nginx/sbin:${PATH}” > /etc/profile.d/nginx.sh
source /etc/profile.d/nginx.sh (该命令表示加载该文件)

8:创建nginx账号并且将目录/data/usr/local/nginx 的属组和属主设置为nginx
该nginx账号为不可登录账号,而且没有加目录
useradd -s /sbin/nologin -r nginx && chown -R nginx:nginx /data/usr/local/nginx

nginx完成安装以后,有四个主要的目录
conf:该目录中保存了nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能,例如fastcgi功能使用的是fastcgi.conf和fastcfi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的方法是将其复制并将default去掉即可
html:该目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的webwenj是默认的错误提示页面
logs:该目录用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面
sbin:该目录存放nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。

10:设置service文件
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/data/usr/local/nginx/logs/nginx.pid

ExecStartPre=/usr/bin/rm -f /data/usr/local/nginx/logs/nginx.pid
ExecStartPre=/data/usr/local/nginx/sbin/nginx -t
ExecStart=/data/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

11:然后就可以启动nginx并设置开机启动了
systemctl daemon-reload
systemctl enable --now nginx

以下是自动安装脚本
#!/bin/bash
echo “本次安装的nginx为1.16.1版本,安装目录为/data/usr/local/nginx”
printf 3
sleep 1
printf “\e[1D"2
sleep 1
printf “\e[1D”“1\n”
basedir=/data/usr/local/nginx
downdir=/data/download
nginxtar=nginx-1.16.1.tar.gz
nginx=echo ${nginxtar} | awk -F ".tar" '{print $1}'
ls ${downdir} &> /dev/null
if [ $? == 0 ];then
wget -P d o w n d i r h t t p s : / / n g i n x . o r g / d o w n l o a d / {downdir} https://nginx.org/download/ downdirhttps://nginx.org/download/{nginxtar}
else
mkdir -p ${downdir}
wget -P d o w n d i r h t t p s : / / n g i n x . o r g / d o w n l o a d / {downdir} https://nginx.org/download/ downdirhttps://nginx.org/download/{nginxtar}
fi
ls -l d o w n d i r / {downdir}/ downdir/{nginxtar} &> /dev/null
if [ $? == 0 ];then
echo “nginx 1.16.1下载完成”
else
echo “nginx下载失败” && exit
fi
{ yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed && echo “依赖包安装完成”; } || { echo “依赖包没有安装完成” && exit; }
cd ${downdir}
tar xf ${nginxtar}
cd n g i n x . / c o n f i g u r e − − p r e f i x = {nginx} ./configure --prefix= nginx./configureprefix={basedir} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install
${basedir}/sbin/nginx -V
if [ KaTeX parse error: Expected 'EOF', got '&' at position 63: …ho "nginx安装失败" &̲& exit fi echo …{basedir}/sbin:${PATH}” > /etc/profile.d/nginx.sh
source /etc/profile.d/nginx.sh
if [ $? == 0 ];then
echo “环境变量设置完成”
fi
useradd -s /sbin/nologin -r nginx && chown -R nginx:nginx ${basedir}
if [ $? == 0 ];then
echo “用户属组及属主设置完成,用户为nginx”
else
echo “用户名及属组设置失败” && exit
fi
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${basedir}/logs/nginx.pid

Nginx will fail to start if /run/nginx.pid already exists but has the wrong

SELinux context. This might happen when running nginx -t from the cmdline.

https://bugzilla.redhat.com/show_bug.cgi?id=1268621

ExecStartPre=/usr/bin/rm -f b a s e d i r / l o g s / n g i n x . p i d E x e c S t a r t P r e = {basedir}/logs/nginx.pid ExecStartPre= basedir/logs/nginx.pidExecStartPre={basedir}/sbin/nginx -t
ExecStart=${basedir}/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now nginx
if [ $? == 0 ];then
echo “nginx已经启动”
else
echo “nginx启动失败” && systemctl status nginx
fi

以下是ubuntu自动安装nginx脚本
#!/bin/bash
echo “本次安装的nginx为1.16.1版本,安装目录为/data/usr/local/nginx”
printf 3
sleep 1
printf “\e[1D"2
sleep 1
printf “\e[1D”“1\n”
basedir=/data/usr/local/nginx
downdir=/data/download
nginxtar=nginx-1.16.1.tar.gz
nginx=echo ${nginxtar} | awk -F ".tar" '{print $1}'
ls ${downdir} &> /dev/null
if [ $? == 0 ];then
echo “a”
wget -P d o w n d i r h t t p s : / / n g i n x . o r g / d o w n l o a d / {downdir} https://nginx.org/download/ downdirhttps://nginx.org/download/{nginxtar}
else
mkdir -p ${downdir}
wget -P d o w n d i r h t t p s : / / n g i n x . o r g / d o w n l o a d / {downdir} https://nginx.org/download/ downdirhttps://nginx.org/download/{nginxtar}
fi
ls -l d o w n d i r / {downdir}/ downdir/{nginxtar} &> /dev/null
if [ $? == 0 ];then
echo “nginx 1.16.1下载完成”
else
echo “nginx下载失败” && exit
fi
{ apt install -y openssl make && echo “依赖包安装完成”; } || { echo “依赖包没有安装完成” && exit; }
cd ${downdir}
tar xf ${nginxtar}
cd n g i n x . / c o n f i g u r e − − p r e f i x = {nginx} ./configure --prefix= nginx./configureprefix={basedir} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install
${basedir}/sbin/nginx -V
if [ KaTeX parse error: Expected 'EOF', got '&' at position 63: …ho "nginx安装失败" &̲& exit fi echo …{basedir}/sbin:${PATH}” > /etc/profile.d/nginx.sh
source /etc/profile.d/nginx.sh
if [ $? == 0 ];then
echo “环境变量设置完成”
fi
id nginx &> /dev/null
if [ $? == 0 ];then
echo “nginx用户已经存在”
else
useradd -s /sbin/nologin -r nginx && chown -R nginx:nginx ${basedir} && echo “新建nginx用户”
fi
cat > /lib/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${basedir}/logs/nginx.pid

ExecStartPre=/bin/rm -f b a s e d i r / l o g s / n g i n x . p i d E x e c S t a r t P r e = {basedir}/logs/nginx.pid ExecStartPre= basedir/logs/nginx.pidExecStartPre={basedir}/sbin/nginx -t
ExecStart=${basedir}/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now nginx
if [ $? == 0 ];then
echo “nginx已经启动”
else
echo “nginx启动失败” && systemctl status nginx
fi

Nginx 是一个高性能、高并发的开源 Web 服务器,它采用事件驱动的异步非阻塞模型,能够快速处理大量并发请求,而且占用资源较少。为了满足不同需求,一些用户可能需要自己编译安装 Nginx编译安装 Nginx 需要进行以下步骤: 1. 首先,需要下载 Nginx 的源代码包。可以从官方网站(https://nginx.org/)上找到最新版本的源代码包,并下载到本地。 2. 下载完成后,将源代码包解压缩到指定的目录中。 3. 接下来,进入到解压缩后的目录中,执行 `./configure` 命令进行配置。该命令会检查当前系统的环境依赖是否满足,并生成构建 Nginx 的 Makefile 文件。 4. 执行 `make` 命令进行编译。该命令会根据保存在 Makefile 文件中的指令,编译生成可执行文件。 5. 编译完成后,执行 `make install` 命令进行安装。该命令会将编译生成的可执行文件、配置文件和其他必要文件拷贝到指定的目录中。 6. 最后,可以通过启动 Nginx 来验证是否安装成功。执行 `nginx` 命令启动 Nginx,通过访问服务器的 IP 地址或域名,可以在浏览器中看到 Nginx 的欢迎页面。 需要注意的是,在进行源码编译安装之前,应该确保系统环境中已经安装了一些必要的依赖库,如 PCRE(Perl Compatible Regular Expressions,用于处理正则表达式)、zlib(用于处理数据压缩)、OpenSSL(用于提供安全的传输层协议支持)等。 以上是基本的 Nginx 源码编译安装的步骤,具体的配置和安装路径可以根据个人需要进行调整。编译安装可以更灵活地配置和定制 Nginx,满足不同的使用和性能要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值