centos6.8上安装nginx

3 篇文章 0 订阅
1 篇文章 0 订阅


在服务器上我们经常会使用到nginx,nginx是C语言开发的,所以建议在linux上运行,此次选择在Centos6.8上安装nginx并运行。

其中Centos6.8是最小化安装,使用的nginx版本为 nginx-1.8.0

一、安装nginx需要的相关环境

1.安装nginx需要下载源码进行编译,编译需要gcc环境,所以需要安装gcc

[root@localhost ~]# yum install gcc-c++
2.安装PCRE,是一个Perl库,包括了兼容的正则表达式。nginx的http模块使用pcre来解析正则表达式。

[root@localhost ~]# yum install -y pcre pcre-devel
3.安装zlib, 提供了多种压缩解压的方式,nginx使用zlib对http包进行gzip。

[root@localhost ~]# yum install -y zlib zlib-devel
4.安装openssl,是一个强大的安全套接字层密码库。nginx不仅支持http协议,还支持https(即在ssl协议上传输http)
[root@localhost ~]# yum install -y openssl openssl-devel

二、编译安装

先将nginx源码拷贝至服务器,解压

[root@localhost ~]# ll
总用量 85312
-rw-------. 1 root root      906 5月   1 20:09 anaconda-ks.cfg
-rw-r--r--. 1 root root    13368 5月   1 20:08 install.log
-rw-r--r--. 1 root root     3482 5月   1 20:07 install.log.syslog
-rw-r--r--. 1 root root   832104 5月   9 23:17 nginx-1.8.0.tar.gz
[root@localhost ~]# tar -zxvf nginx-1.8.0.tar.gz 
[root@localhost ~]# cd nginx-1.8.0
在此目录下会有名为configure的可执行文件

使用 ./configure --help 会看到详细的参数,具体配置参数用法视情况而定,这里至配置nginx的路径

[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx
执行安装:

[root@localhost nginx-1.8.0]# make
[root@localhost nginx-1.8.0]# make install
此时在 /usr/local 下 已经存在nginx文件夹。


三、启动nginx


[root@localhost nginx]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx
查看nginx进程

[root@localhost sbin]# ps aux | grep nginx
root       3685  0.0  0.1  23852   796 ?        Ss   23:37   0:00 nginx: master process ./nginx
nobody     3686  0.0  0.2  24280  1392 ?        S    23:37   0:00 nginx: worker process
root       3688  0.0  0.1 103336   896 pts/0    S+   23:38   0:00 grep nginx
3685是nginx主进程的进程id,3686是nginx工作进程的进程id

注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:

./nginx -c /usr/local/nginx/conf/nginx.conf

如果不指定-cnginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))

此时通过浏览器输入ip访问,出现nginx欢迎页面表示nginx启动成功了。(访问时关闭系统防火墙,在centos6.8最小化安装中防火墙只开启22端口,并没有开启80端口,所以需要关闭防火墙:service iptables stop)


四、停止、重启nginx

停止:

在/usr/local/nginx/sbin中执行 

1、 ./nginx -s stop  相当于先查出nginx进程id再使用kill命令强制杀掉进程

2、 ./nginx -s quit  待nginx进程处理任务完毕后进行停止

重启:

1.先停止再重启

./nginx -s quit

./nginx

2.重新加载配置文件

./nginx -s reload


下面是在一篇网上的手册上看到的关于nginx开机自启的脚本,

vi /etc/init.d/nginx

#!/bin/bash
#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


chmod a+x /etc/init.d/nginx 所有用户可执行

加入到rc.local文件中: vi /etc/rc.local 加入一行  /etc/init.d/nginx start   保存退出后,下次重启会生效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值