centos6.4下的Nginx安装

时间:2022-08-15 10:24:32

1、准备工作

选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL。
Nginx是C写的,需要用GCC编译;Nginx的Rewrite和HTTP模块会用到PCRE;Nginx中的Gzip用到zlib;
用命令“gcc”,查看gcc是否安装;如果出现“gcc: no input files”信息,说明已经安装好了。
否则,就需要用命令“yum install gcc”,进行安装了!一路可能需要多次输入y,进行确认。
安装好后,可以再用命令“gcc”测试,或者用命令“gcc -v”查看其版本号。
同样方法,用如下命令安装PCRE,zlib,OpenSSL(其中devel,是develop开发包的意思):

yum install -y pcre pcre-devel  
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

2、创建Nginx目录下载安装

1、cd /usr/local(转到local目录下)
2、mkdir nginx(创建Nginx文件夹)
3//下载Nginx包或者从window上传
4、tar -zxvf nginx-1.8.0.tar.gz(nginx包名)(解压)
5./configure(配置)
6、make(编译)
7、make install (安装)

默认的安装路径为:/usr/local/nginx;跳转到其目录下sbin路径下,便可以启动或停止它了。
如果需要查找它的安装路径使用

whereis nginx

3、启动nginx

//注意:这里关闭防火墙的命令是在centos6.4状态下的,针对centos7以上的是用其他的命令。这里就不解释了。
service iptables stop(关闭防火墙)
service iptables status(验证防火墙的状态)
chkconfig iptables off(关闭防火墙的自动运行)
chkconfig --list | grep iptables(验证)

cd /usr/local//nginx/sbin/(转到启动目录下)
./nginx(启动)
./nginx -s stop(停止)

4、测试

使用“localhost”注意Nginx的端口为80;
出现“welcome to Nginx“就成功了!!

5、开机启动

编写shell脚本
这里使用的是编写shell脚本的方式来处理

vi /etc/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=/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

保存退出“ESC键 、“:wq”回车”;

设置文件的访问权限
chmod a+x /etc/init.d/nginx 

加入到rc.local文件中

vi /etc/rc.local

加入一行 /etc/init.d/nginx start 保存并退出,下次重启会生效。