Centos6下部署Nginx的方法
本篇内容主要讲解“Centos6下部署Nginx的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Centos6下部署Nginx的方法”吧!
10余年建站经验, 成都网站制作、成都网站设计客户的见证与正确选择。创新互联建站提供完善的营销型网页建站明细报价表。后期开发更加便捷高效,我们致力于追求更美、更快、更规范。
1:环境部署工具及版本:
Nignx:tengine-2.2.2.tar.gz
2:Nginx配置
1.解压
tar zxvf tengine-2.2.2.tar.gz
2.配置检查
进入解压后的目录 ./configure
错误1:
[root@localhost tengine-2.2.2]# ./configure
checking for OS
+ Linux 2.6.32-642.el6.x86_64 x86_64
checking for C compiler ... not found
解决:
yum -y install gcc gcc-c++ autoconf automake make
错误2:
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
解决:
yum install pcre-devel
错误3:
checking for PCRE JIT support ... not found
checking for OpenSSL library ... not found
解决:
yum -y install openssl openssl-devel
再次检查./configure是否报错
3.安装
make && make install 安装完成 安装目录为/usr/local/nginx
找到 /usr/local/nginx/sbin/nginx -V 查看所有加载的模块
/usr/local/nginx/sbin/nginx start|stop|restart 启动和关闭ngix服务
访问 http://ip地址
4.nignx命令参数
nginx -m 显示所有加载的模块
nginx -l 显示所有可以使用的指令
nginx -t 检查nginx的配置文件是否正确
nginx -s start 启动nginx
nginx -s reload 重启nginx
nginx -s stop 停止nginx
5.开机启动nginx
第一步:nignx命令文件移动到 /etc/init.d/目录下
nginx命令文件内容:
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: NGINX is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -n "$user" ]; then if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done fi } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
第二步:赋权限chmod 775 nginx
第三步:添加到系统服务 设置开机启动
chkconfig --add nginx
chkconfig nginx on
第四步:
service nginx status 检查状态
service nginx stop 停止服务
service nginx start 启动服务
service nginx restart 启动服务
6.80端口永久开放
第一步:/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
第二步:service iptables save
7.前后台动静分离配置,/usr/local/nginx/conf/nginx.conf vi修改
location / {
root /var/www/xxx; --前端项目xxx丢到www目录下
index index.html index.htm;
}
location ~/datav {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
proxy_pass http://127.0.0.1:8888;
}
8.重启服务service nginx restart,使nginx新配置生效
到此,相信大家对“Centos6下部署Nginx的方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
文章标题:Centos6下部署Nginx的方法
文章起源:http://cdiso.cn/article/pgdsoi.html