目录
编译安装的步骤
configure #检查当前编译环境,生成MakeFile文件,编译的时候指定某些参数,没有找到相关的模块,是执行不过去的(报错)
make #根据MakeFile生成相关模块
make install #将模块拷贝到指定目录
一、Nginx编译安装
1、Nginx编译安装依赖下载
#Centos8
yum -y install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
#Centos7
yum -y install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
#Ubuntu
apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
2、创建用户nginx
[root@centos7 ~]# useradd -s /sbin/nologin -r nginx
3、安装Nginx
[root@centos7 ~]# cd /usr/local/src/
[root@centos7 src]# ll
total 10668
-rw-r--r-- 1 root root 1039530 Apr 21 2020 nginx-1.18.0.tar.gz
[root@centos7 src]# tar -xvf nginx-1.18.0.tar.gz
[root@centos7 src]# cd nginx-1.18.0/
#开始编译
./configure \
--prefix=/apps/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 \
--with-file-aio
make
make install
编译选项说明:--with-http_ssl_module 开启ssl模块;--with-http_v2_module开启http 2.0版本模块;--with-stream支持tcp/udp;--user=nginx和--group=nginx可以不用指定,后期再进行修改;--with-http_realip_module开启透传IP;--with-http_stub_status_module开启状态页功能,用于监控;--with-http_gzip_static_module开启压缩功能;--with-pcre在Nginx编译需要 PCRE(Perl Compatible Regular Expression),因为Nginx 的Rewrite模块和HTTP 核⼼模块会使⽤到PCRE正则表达式语法;
4、修改PATH
echo "PATH=/apps/nginx/sbin:${PATH}" > /etc/profile.d/nginx.sh
5、Nginx启动文件
[root@centos7 ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
ExecStartPre=/bin/rm -f /apps/nginx/logs/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
6、启动服务
systemctl daemon-reload #加载Nginx启动文件
systemctl enable --now nginx #开启服务器 或者 执行nginx
[root@centos7 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
二、Nginx四个主要目录
[root@centos7 ~]# tree /apps/nginx/
/apps/nginx/
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs
│ ├── access.log
│ ├── error.log
│ └── nginx.pid
└── sbin
└── nginx
conf:该⽬录中保存了nginx所有的配置⽂件,其中nginx.conf是nginx服务器的最核⼼最主要的配置⽂件,其他的.conf则是⽤来配置nginx相关的功能的,例如fastcgi功能使⽤的是fastcgi.conf和fastcgi_params两个⽂件,配置⽂件⼀般都有个样板配置⽂件,是⽂件名.default结尾,使⽤的使⽤将其复制为并将default去掉即可。
html:该⽬录中保存了nginx服务器的web⽂件,但是可以更改为其他⽬录保存web⽂件,另外还有⼀个50x的web⽂件是默认的错误⻚⾯提示⻚⾯。
logs:该⽬录⽤来保存nginx服务器的访问⽇志、错误⽇志等⽇志,logs⽬录可以放在其他路径,⽐如/var/logs/nginx⾥⾯。
sbin:该⽬录⽤来保存nginx⼆进制启动脚本,可以接受不同的参数以实现不同的功能。
三、shell脚本一键编译安装Nginx服务
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'
NGINX_URL=http://nginx.org/download/
NGINX_FILE=nginx-1.18.0.tar.gz
ECHO_NGINX=echo-nginx-module-0.62.tar.gz
OPENSSL=openssl-1.1.1j.tar.gz
NGINX_INSTALL_DIR=/apps/nginx
CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`
os(){
if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install redhat-lsb-core &> /dev/null; }
fi
OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
}
check_file (){
cd ${SRC_DIR}
rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
if [ ! -e ${NGINX_FILE} ];then
${COLOR}"缺少${NGINX_FILE}文件"${END}
${COLOR}'开始下载NGINX源码包'${END}
wget ${NGINX_URL}${NGINX_FILE} || { ${COLOR}"NGINX源码包下载失败"${END}; exit; }
elif [ ! -e ${ECHO_NGINX} ];then
${COLOR}"缺少${ECHO_NGINX}文件"${END}
exit
elif [ ! -e ${OPENSSL} ];then
${COLOR}"缺少${OPENSSL}文件"${END}
exit
else
${COLOR}"相关文件已准备好"${END}
fi
}
install_nginx(){
${COLOR}"开始安装NGINX"${END}
id nginx &> /dev/null || { useradd -s /sbin/nologin -r nginx; $COLOR"创建nginx用户"$END; }
${COLOR}"开始安装NGINX依赖包"${END}
if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
yum -y install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed &> /dev/null
elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then
yum -y install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed &> /dev/null
else
apt update &> /dev/null;apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev &> /dev/null
fi
cd $SRC_DIR
tar xf ${NGINX_FILE} && tar xf ${ECHO_NGINX} && tar xf ${OPENSSL}
NGINX_DIR=`echo ${NGINX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
ECHO_NGINX_DIR=`echo ${ECHO_NGINX}| sed -nr 's/^(.*[0-9]).*/\1/p'`
OPENSSL_DIR=`echo ${OPENSSL}| sed -nr 's/^(.*[0-9][a-z]).*/\1/p'`
cd ${NGINX_DIR}
./configure --prefix=${NGINX_INSTALL_DIR} --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 --with-file-aio --add-module=${SRC_DIR}/${ECHO_NGINX_DIR} --with-openssl=${SRC_DIR}/${OPENSSL_DIR}
make -j $CPUS && make install
[ $? -eq 0 ] && $COLOR"NGINX编译安装成功"$END || { $COLOR"NGINX编译安装失败,退出!"$END;exit; }
echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
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=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/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 &> /dev/null
systemctl is-active nginx &> /dev/null || { ${COLOR}"NGINX 启动失败,退出!"${END} ; exit; }
${COLOR}"NGINX安装完成"${END}
}
main(){
os
check_file
install_nginx
}
main