服务器快速集成Nginx
1.安装依赖
1.1.pcre重定向依赖
yum -y install pcre*
- PCRE(Perl Compatible Regular Expressions)是一个Perl库,不止具有http重定向依赖,还包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
1.2.openssl(http/https支持,如果不需要https可以不安装。
yum -y install pcre*
- OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
1.3.安装 Tengine 执行配置命令 gcc环境
yum -y install gcc gcc-c++ autoconf automake make
- 若是本机上已经安装则可以忽略,主要用于编译Nginx源码
2.下载解压
2.1.下载
wget http://nginx.org/download/nginx-1.12.2.tar.gz
2.2.解压
先创建Nginx存放路径,再进行解压
cd /usr/local/
mkdir nginxgz
tar -zxvf nginx-1.7.8.tar.gz -C /usr/local/nginxgz
3.编译安装Nginx
cd /usr/local/nginxgz/nginx-1.12.2
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre
- –with-http_ssl_module : 支持https
- –with-http_stub_status_module : 支持nginx状态查询
- –with-http_v2_module : 支持Google发明的SPDY协议,必须有ssl的支持
- –with-pcre : 支持rewrite重写功能,必须有pcre
注:若是没有安装gcc则会报如下错误:
checking for OS
+ Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... not found
编译:
make
make install
4.操作Nginx
- 启动:
/usr/local/nginx/sbin/nginx
- 重启:
/usr/local/nginx/sbin/nginx -s reload
- 关闭:
/usr/local/nginx/sbin/nginx -s stop
5.配置防火墙(iptables)测试
5.1.关闭防火墙
service iptables stop
5.2.编辑配置文件
vi /etc/sysconfig/iptables
5.3.开发80端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
5.4.重启服务
service iptables restart
测试,浏览器输入服务器地址(如:192.168.1.111)
6.将Nginx添加到服务(service)中
- 6.1.切换到启动脚本
cd /etc/init.d
- 6.2.创建nginx文件
touch nginx
- 6.3.编辑nginx文件
vim /etc/init.d/nginx
- 6.4.并且添加以下内容
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
#
# 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/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 /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
$nginxd -s reload
#if your nginx version is below 0.8, please use this command: "kill -HUP `cat ${nginx_pid}`"
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
注:
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
- 脚本中这两个不能去除,不然不能启动,而且会报以下错误,即语法错误
nginx 服务不支持 chkconfig
- chkconfig语法:
语 法:chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代号>][系统服务][on/off/reset]
- 6.5.保存退出后,切换路径
cd /etc/rc.d/init.d
- 6.6.设置权限
chmod +x nginx
- 6.7.chkconfig改变nginx运行级别
/sbin/chkconfig --level 345 nginx on
到此为止:
任何位置都能运行 service nginx start 可选 start | stop | restart | reload | status | help