1. 编译安装nginx
先安装对应的依赖包
yum -y install make gcc gcc-c++ flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel gd freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel unzip libcap lsof
去官网下载对应的nginx源码包
www.nginx.org
在安装Nginx需要用到pcre程序,因此需要下载pcre程序包
https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
安装epel扩展yum源
rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum clean all && yum list
所需的依赖
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*
创建Nginx运行用户
useradd -M -s /sbin/nologin nginx
将两个源码包解压到 /usr/local/src
中
tar xf pcre-8.42.tar.gz -C /usr/local/src/
perc解压即可,不用安装,Nginx安装时指定pcre的解压路径即可
tar zxf nginx-1.20.1.tar.gz -C /usr/local/src
进入/usr/local/src/nginx-1.20.1
目录下
cd /usr/local/src/nginx-1.20.1
编译并安装
./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.42 --user=nginx --group=nginx
make -j 4 && make install
安装完成后可以查看nginx目录下的文件,主要有以下四个
ll /usr/local/nginx
备份conf文件
cp /usr/local/nginx/conf/nginx.conf{,.bak}
修改用户
vim /usr/local/nginx/conf/nginx.conf
将#user nobody
改为 user nginx
启用PHP支持
启动nginx服务
/usr/local/nginx/sbin/nginx
编辑配置环境变量,让系统读到Nginx路径
vim /etc/profile.d/nginx.sh
加入
export PATH=/usr/local/nginx/sbin:$PATH
生成服务启动脚本
vim /etc/init.d/nginx
加入
#!/bin/bash # chkconfig: - 99 2 # description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -3 $(cat $PIDF) ;; restart) $0 stop &> /dev/null if [ $? -ne 0 ] ; then continue ; fi $0 start ;; reload) kill -1 $(cat $PIDF) ;; *) echo "Userage: $0 { start | stop | restart | reload }" exit 1 esac exit 0
配置服务开机自动启动
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
重启nginx
nginx -s reload
http://你的域名 浏览器访问就可以看到 nginx 安装成功