Linux Nginx安装部署

Linux Nginx安装部署

1、Nginx简介

Nginx官网:http://nginx.org/

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,期初开发的目的就是为了代理电子邮件服务器室友:Igor Sysoev开发,源代码符合BSD开源。其特点就是占用内存少并发能力强,使用Nginx的大型网站已经有很多:百度、淘宝、腾讯等等...

Nginx作为Http服务器,有以下几项基本特征:

a、 处理静态文件,索引文件以及自动索引,打开文件描述符缓冲;
b、 无缓存的反向代理加速,简单的负载均衡和容错;   
c、 模块化的结构,包括gzipping,byte ranges,chunked responses以及SSI-filter等filter,如果由FastCGI或其它代理服务器处理蛋液中存在的多个SSI,则这项处理可以并行运行,而不需要相互等待。
d、 支持SSL和TLSSNI。

2、官网下载源码包

nginx-1.12.1.tar.gz 

下载地址:http://nginx.org/en/download.html

3、 准备工作

Nginx的安装依赖于以下三个包

a、SSL功能需要openssl库,http://www.openssl.org/
b、gzip模块需要zlib库,http://www.zlib.net/
c、rewrite模块需要pcre库,http://www.pcre.org/

按顺序安装上述软件软件,若已安装即可跳过

3.1、源码安装zlib

下载地址:http://www.zlib.net/

tar -zvxf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install        

3.2、源码安装openssl

下载地址:https://www.openssl.org/source/

tar -zvxf openssl-1.1.0f.tar.gz
cd openssl-1.1.0f
./config
make
make install

3.3、源码安装pcre

下载地址:http://www.pcre.org/

tar -zvxf pcre-8.40.tar.gz
cd pcre-8.40
./configure --prefix=/usr/local/pcre
make
make install

如果是64位系统,需要在安装目录下建立lib64目录,拷贝lib下的所有动态库到lib64.

cd /usr/local/pcre/
mkdir lib64
cd lib
cp -R * /usr/local/pcre/lib64/

4、 安装Nginx服务,把Nginx安装到/usr/local/nginx目录下

解压安装包:

[root@QIANZI-BASE home]# tar -zvxf nginx-1.12.1.tar.gz

[root@QIANZI-BASE home]# cd nginx-1.12.1

编译安装:

[root@QIANZI-BASE nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=../pcre-8.40 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.0f

[root@QIANZI-BASE nginx-1.12.1]# make

[root@QIANZI-BASE nginx-1.12.1]# make install

注:pcre-8.40 , zlib-1.2.11 , openssl-1.1.0f 分别为pcre, zlib, openssl的源码目录。

5、验证Nginx是否安装成功

[root@QIANZI-BASE nginx-1.12.1]# /usr/local/nginx/sbin

[root@QIANZI-BASE sbin]# ./nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

浏览器中输入:http://192.168.10.188/

输出:Welcome to nginx!
     If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
     For online documentation and support please refer to nginx.org.
     Commercial support is available at nginx.com.
     Thank you for using nginx.

安装成功!

6、将Nginx设置为开机自动启动

echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local

7、使用server命令启动nginx服务

在/etc/init.d下创建nginx启动脚本:

[root@QIANZI-BASE init.d]# vim nginx
#!/bin/sh 
# 
# 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 
# config: /etc/sysconfig/nginx 
# pidfile: /var/run/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/subsys/nginx 

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 
    killall -9 nginx 
} 

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
修改脚本权限

[root@QIANZI-BASE init.d]# chmod 755 nginx

将脚本文件加入到chkconfig中

[root@QIANZI-BASE init.d]# chkconfig --add nginx

设置nginx开机在3和5级别自动启动

[root@QIANZI-BASE init.d]# chkconfig --level 35 nginx on

测试nginx脚本是否能够正常使用

/etc/init.d/nginx restart
/etc/init.d/nginx reload
/etc/init.d/nginx stop 

8、[完]

你可能感兴趣的:(nginx,Nginx)