当前位置 主页 > 服务器问题 > nginx问题汇总 >

    centos6.5服务器安装Nginx设置服务和开机自启的方法(2)

    栏目:nginx问题汇总 时间:2018-10-19 16:46

    其实在写这个的时候必须注意的是,不管什么应用程序端口不能冲突!比如说我的nginx是绑定的80端口,如果tomcat再设定80端口,那么我的设置就算是绑定到localhost去也是会转发失败的!毕竟网络端口只能一个应用程序占用。

    <!--检查我们的设置是否正确,正确或者错误都有对应的提示-->/opt/nginx/sbin/nginx -t<!--配置正确,则启用nginx-->/opt/nginx/sbin/nginx<!--重新载入配置文件-->/opt/nginx/sbin/nginx -t<!--当然到了这里的时候肯定还不能通行,毕竟我们防火墙还把443端口拦截的,所以接着走起来。--><!--添加443端口到防火墙-->/sbin/iptables -I INPUT -p tcp --dport 443 -j ACCEPT<!--保存防火墙配置-->/etc/rc.d/init.d/iptables save<!--是配置文件生效-->/etc/init.d/iptables status

    走到这一步,我们可以测试一下服务器了,按照正常的来讲,我现在的服务器已经是http和https都已经完全支持了。

    3、设置服务和自启

    其实说来,这里基本也没啥注意的,只要nginx路径设置正确即可。

    #!/bin/sh# Name:nginx4comex# nginx - this script starts and stops the nginx daemon## description: Nginx is an HTTP(S) server, HTTP(S) reverse \#        proxy and IMAP/POP3 proxy server# processname: nginx# config:   /opt/nginx/conf/nginx.conf# pidfile:   /comexHome/nginx/nginx.pid## Created By http://comexchan.cnblogs.com/# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx"NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"NGINX_LOCK_FILE="/var/lock/subsys/nginx4comex"prog=$(basename $NGINX_DAEMON_PATH)start() {  [ -x $NGINX_DAEMON_PATH ] || exit 5  [ -f $NGINX_CONF_FILE ] || exit 6  echo -n $"Starting $prog: "  daemon $NGINX_DAEMON_PATH -c $NGINX_CONF_FILE  retval=$?  echo  [ $retval -eq 0 ] && touch $NGINX_LOCK_FILE  return $retval}stop() {  echo -n $"Stopping $prog: "  killproc $prog -QUIT  retval=$?  echo  [ $retval -eq 0 ] && rm -f $NGINX_LOCK_FILE  return $retval}restart() {  configtest || return $?  stop  start}reload() {  configtest || return $?  echo -n $"Reloading $prog: "  killproc $NGINX_DAEMON_PATH -HUP  RETVAL=$?  echo}force_reload() {  restart}configtest() { $NGINX_DAEMON_PATH -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 2esac

    上面的代码就是用来创建服务的代码,将他们保存在nginx4comex文件中(这个文件我仍在了/opt/nginx目录下,一样使用vim编写)。注意下面的代码和你的配置对应即可。

    NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx"NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"