以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./configure−−prefix={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./configure−−prefix={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