nginx 安装
准备编译安装的基础环境
yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-develsystemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
下载nginx源码包
[root@s2 ~]# cd /usr/local/src/
[root@s2 src]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@s2 src]# tar xf nginx-1.16.1.tar.gz
[root@s2 src]# cd nginx-1.16.1/
编译是为了检查系统环境是否符合编译安装的要求,比如是否有gcc编译工具,是否支持编译参数当中的模块,并根据开启的参数等生成Makefile文件为下一步做准备:
[root@s2 nginx-1.16.1]#./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
[root@s2 nginx-1.16.1]# make #编译步骤,根据Makefile文件生成相应的模块
[root@s2 nginx-1.16.1]# make install #创建目录,并将生成的模块和文件复制到相应的目录
[root@s2 nginx-1.16.1]# useradd nginx -s /sbin/nologin -u 2000 #以普通用户启动nginx
[root@s2 nginx-1.16.1]# chown nginx.nginx -R /apps/nginx/
备注: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二进制启动脚本,可以接受不同的参数以实现不同的功能。
验证版本及编译参数:
[root@s2 nginx-1.16.1]# /apps/nginx/sbin/nginx -V
创建Nginx自启动脚本:
[root@s1 ~]# cat /usr/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/run/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 /apps/nginx/run/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
验证Nginx自启动脚本:
[root@s2 nginx-1.16.1]# systemctl daemon-reload
[root@s2 nginx-1.16.1]# systemctl start nginx
[root@s2 nginx-1.16.1]# systemctl enable nginx
[root@s2 nginx-1.16.1]# systemctl status nginx
一键安装nginx脚本
#!/bin/bash
systemctl stop firewalld && setenforce 0
echo "安装nginx依赖包"
yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-develsystemd-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 "下载nginx源码包"
cd /usr/local/src/
if [ ! -d "/usr/local/src/nginx-1.16.1" ]; then
wget https://nginx.org/download/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd nginx-1.16.1/
else
echo "此文件已存在"
exit
fi
echo "开始编译安装nginx"
./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
[ $? -eq 0 ] && echo "nginx install compleate,go next step." || echo "nginx install failed ,please check"
make && make install &> /dev/null
echo "创建普通用户"
useradd nginx -s /sbin/nologin -u 2000
echo "修改nginx属主和属组"
chown nginx.nginx -R /apps/nginx/
echo "启动nginx"
/apps/nginx/sbin/nginx