centos6安装nginx

2018-11-22  本文已影响9人  超鸽带你飞

nginx的牛逼之处就不用多说了,反正一个字:牛逼!

先安装编译工具

yum -y install gcc gcc-c++ make

A. 依耐安装

nginx 依赖于 zlib pcre ssl 三个模块,

有两种方式:

  1. yum安装
    yum -y install zlib-devel pcre-devel openssl-devel

  2. 编译安装
    cd /opt

wget https://www.openssl.org/source/openssl-1.0.2q.tar.gz
./config --prefix=/usr/local/openssl
make && make install
/usr/local/openssl/bin/openssl version
官网下载地址: https://ftp.pcre.org/pub/pcre/   
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
./configure && make && make install
wget http://zlib.net/zlib-1.2.11.tar.gz
./configure && make && make install

建议安装方式:

yum安装zlib和pcre
yum -y install zlib-devel pcre-devel
编译安转openssl

B. nginx安装

wget http://nginx.org/download/nginx-1.10.2.tar.gz
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-openssl=/opt/openssl-1.0.2q
make && make install

注意:
1. --prefix 可以省略,默认安装在/usr/local/nginx
2. 这3个扩展的目录是他们的源代码目录,不是安装目录,这点很容易搞错。
--with-pcre=/opt/pcre-8.32 \ 
--with-zlib=/opt/zlib-1.2.7 \ 
--with-openssl=/opt/openssl-1.0.2q

启动 nginx

/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload

报错了(编译安装的pcre),error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory,按照下面方式解决

1.用whereis libpcre.so.1命令找到libpcre.so.1在哪里
2.用ln -s /usr/local/lib/libpcre.so.1 /lib64命令做个软连接就可以了
3.用sbin/nginx启动Nginx
4.用ps -aux | grep nginx查看状态

[root@localhost nginx]# whereis libpcre.so.1
[root@localhost nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[root@localhost nginx]# sbin/nginx
[root@localhost nginx]# ps -aux | grep nginx
  1. 查看端口
netstat -tnl|grep 8080
tcp        0      0 0.0.0.0:8080        0.0.0.0:*       LISTEN
  1. 浏览器访问:
    localhost

  2. 开启nginx的iptables限制

vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

然后,重启下 iptables。
service iptables restart

C. 开启自启动nginx

开机启动的配置文件是:/etc/rc.local
vim 末尾加入 /usr/local/nginx/sbin/nginx 即可

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/usr/local/bin/redis-server /etc/redis.conf
/usr/local/php/sbin/php-fpm
/usr/local/nginx/nginx

配置启动脚本

启动脚本 (见最后)
vim /etc/init.d/nginx
chmod -R 755 /etc/init.d/nginx
vim /etc/init.d/nginx
chkconfig nginx on   开机启动
chkconfig --list

#!/bin/bash
# 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
# pidfile:     /var/run/nginx/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/nginx.lock

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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

参考:

上一篇 下一篇

猜你喜欢

热点阅读