一、准备工作

1、开放防火墙端口

vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT                   ##增加
:wq!                         #保存
/etc/init.d/iptables restart   #重启防火墙生效

2、关闭SELINUX

vi /etc/selinux/config   #编辑配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq #保存
shutdown -r now #重启系统

3、安装编译工具及库文件

yum install make apr* autoconf automake curl curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat*  cpp glibc libgomp libstdc++-devel keyutils-libs-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch

二、下载所需文件

1、pcre : https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz

2、nginx : http://nginx.org/download/nginx-1.11.10.tar.gz

三、安装

1、安装pcre

mkdir /usr/local/pcre                  #创建安装文件夹
tar zxvf pcre-8.40.tar.gz              #解压包文件
cd pcre-8.40
./configure --prefix=/usr/local/pcre              #配置编译文件
make && make instal                          #编译并安装

2、安装nginx

groupadd www              #添加www组
useradd -g www www -s /bin/false     #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统
cd nginx-1.11.10
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module  --with-http_gzip_static_module --with-pcre=/usr/local/src/pcre-8.40          #编译配置文件,注意:--with-pcre=/usr/local/src/pcre-8.30指向的是源码包解压的路径,而不是安装的路径,否则会报错
make
make install
/usr/local/nginx/sbin/nginx  #启动Nginx

 

设置nginx开启启动
vi /etc/rc.d/init.d/nginx    #编辑启动文件添加下面内容
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
.  /etc/rc.d/init.d/functions
# Source networking configuration.
.  /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;

status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL


-----------------------------------------------------
:wq!  保存

 

chmod 775 /etc/rc.d/init.d/nginx  #赋予文件执行权限
chkconfig nginx on    #设置开机启动
/etc/rc.d/init.d/nginx restart #重启Nginx