龙空技术网

centos7稳定nginx搭建,快速搭建nginx

元宇宙hryc 318

前言:

如今我们对“centos7libgcj”可能比较珍视,小伙伴们都想要剖析一些“centos7libgcj”的相关知识。那么小编也在网络上收集了一些有关“centos7libgcj””的相关文章,希望我们能喜欢,你们快快来学习一下吧!

程序人生地址:

一、安装准备

首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 所以执行如下命令安装

$ yum install gcc-c++

$ yum install pcre pcre-devel

$ yum install zlib zlib-devel

$ yum install openssl openssl-devel

二、安装Nginx

安装之前,最好检查一下是否已经安装有nginx

$ find -name nginx

如果系统已经安装了nginx,那么就先卸载

$ yum remove nginx

首先进入/data目录

$ cd /data

从官网下载最新版的nginx

$ wget

解压nginx压缩包

$ tar -zxvf nginx-1.11.10.tar.gz

删除下载的nginx

$ rm -rf nginx-1.11.10.tar.gz

会产生一个nginx-1.11.10 目录,这时进入nginx-1.11.10目录

$ mv nginx-1.11.10 nginx

$ cd nginx

接下来安装,使用--prefix参数指定nginx安装的目录,make、make install安装

./configure --prefix=/data/nginx --conf-path=/data/nginx/nginx.conf

1.$ ./configure --prefix=/data/nginx --conf-path=/data/nginx/nginx.conf

$默认安装在/usr/local/nginx

2.$ make

3.$ make install

如果没有报错,顺利完成后,最好看一下nginx的安装目录

$ whereis nginx

nginx安装在了/data/nginx目录下

默认的安装路径为:/data/nginx;跳转到其目录下sbin路径下,便可以启动或停止它了。

启动:/data/nginx/sbin/nginx

关闭:/data/nginx/sbin/nginx -s stop

重启: /data/nginx/sbin/nginx -c /home/zhyg/nginx/nginx.conf

防火墙开放80端口

增加80端口到防火墙配置中,执行以下操作:

[root@localhost ~]/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #开启80端口

[root@localhost ~]/etc/rc.d/init.d/iptables save #保存配置 service iptables save

[root@localhost ~]/etc/rc.d/init.d/iptables restart #重启防火墙 service iptables restart

3、添加到系统服务

使用命令“# vi /etc/init.d/nginx”,打开编辑器,输入如下内容:

#!/bin/sh # chkconfig: 2345 85 15 # Startup script for the nginx Web Server # chkconfig: - 85 15# description: nginx is a World Wide Web server. It is used to serve# description: nginx is a World Wide Web server. # It is used to serve HTML files and CGI. # processname: nginx # pidfile: /data/nginx/logs/nginx.pid # config: /data/nginx/conf/nginx.conf   PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx deamon" NAME=nginx DAEMON=/data/nginx/sbin/$NAME SCRIPTNAME=/etc/init.d/$NAME  test -x $DAEMON || exit 0  d_start(){  $DAEMON || echo -n "already running"  }  d_stop(){  $DAEMON -s quit || echo -n "not running" }    d_reload(){  $DAEMON -s reload || echo -n "can not reload"  }  case "$1" instart)  echo -n "Starting Ngnix"  d_start  echo "." ;;  stop)  echo -n "Stopping Ngnix"  d_stop  echo ".";;reload)  echo -n "Reloading $DESC conf..."  d_reload  echo "reload .";;restart)  echo -n "Restarting Ngnix"  d_stop  sleep 2  d_start  echo ".";;*)  echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2  exit 3;;esac  exit 0

保存退出后,再使用下面的命令,使其可执行;然后,添加配置并查看。

可用chkconfig修改其值,也可用ntsysv工具改变是否自启动。

# chmod +x /etc/init.d/nginx

# chkconfig --add nginx

# chkconfig nginx on/off

# chkconfig --list nginx

nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

启动 service nginx start

停止 service nginx stop

重启 service nginx restart

安装完毕后,进入安装后目录(/data /nginx)便可以启动或停止它了。

到此,使用CentOS安装nginx已经完成了。

nginx 配置路径重定向地址:

location / {

rewrite ^(.*) break;

}

新版配置:

#!/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=/var/run/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: "

#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

把这个文档放到/etc/init.d/

然后执行

chkconfig --add /etc/init.d/nginx

chmod 755 /etc/init.d/nginx

chkconfig --add nginx

启动:/data/nginx/sbin/nginx

关闭:/data/nginx/sbin/nginx -s stop

查看进程:ps -A | grep nginx

原文地址:

标签: #centos7libgcj