美文网首页
centos6安装nginx

centos6安装nginx

作者: 超鸽带你飞 | 来源:发表于2018-11-22 10:55 被阅读9次

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

先安装编译工具

yum -y install gcc gcc-c++ make

A. 依耐安装

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

  • zlib:nginx提供gzip模块,需要zlib库支持 ;
  • openssl:nginx提供ssl功能 ;
  • pcre:支持地址重写rewrite功能,支持nginx伪静态

有两种方式:

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

  2. 编译安装
    cd /opt

  • openssl编译
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
  • pcre编译(依耐8版本)
官网下载地址: https://ftp.pcre.org/pub/pcre/   
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
./configure && make && make install
  • zlib编译
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

参考:

相关文章

  • Linux安装(nginx、Git)

    安装nginx(Centos6) 源码安装nginx 下载nginx wget http://nginx.org/...

  • CentOS6.5部署Gitlab-CE代码托管服务

    安装要求 CentOS6安装参考 安装(这里使用HTTPS) 使用外部Nginx、配置Email Nginx配置(...

  • CentOS6 部署Django+Nginx+uwsgi

    CentOS安装Django + Nginx + uwsgi 软件环境 MySQL CentOS6 Nginx 安...

  • centos6 通过yum安装nginx

    本文主要介绍centos6通过yum安装nginx的相关步骤,linux安装nginx以及配置,具有一定的参考价值...

  • Nginx

    nginx3个核心模块: centos6 源码安装 Nginx后做开机自启:方法一:在 rc.local 中...

  • centos6安装nginx

    1.安装环境 1、需要安装gcc的环境。 2、第三方的开发包。 PCREPCRE(Perl Compatible ...

  • centos6安装nginx

    nginx的牛逼之处就不用多说了,反正一个字:牛逼! 先安装编译工具 yum -y install gcc gcc...

  • Centos6 安装nginx

    安装相关工具 创建配置文件/etc/yum.repos.d/nginx.repo,输入如下内容: 默认使用的是稳定...

  • Centos6 Nginx安装

    1.下载nginx 方法一 wget http://nginx.org/download/nginx-1.11.6...

  • Centos6 Nginx安装

    1.下载nginx 方法一 wget http://nginx.org/download/nginx-1.11.6...

网友评论

      本文标题:centos6安装nginx

      本文链接:https://www.haomeiwen.com/subject/qabkqqtx.html