码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx源代码安装

时间:2015-06-20 14:29:07      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:nginx   源代码   编译安装   

Linux下安装软件有三种方式,这里我以源代码编译安装为主。服务器最小化安装后,安装依赖包。
 
出于管理和安全的目的,我们希望使用一个指定的普通用户身份去运行我们的Web服务器。所以,我们首先增加一个普通用户用于运行我们的Nginx。

[root@master ~]# groupadd nginx 
[root@master ~]# useradd -g nginx nginx

关闭系统防火墙,

[root@master ~]# service iptables stop 
[root@master ~]# chkconfig iptables off

 

1. 下载最新稳定版本并安装Nginx
然后下载、解压并编译安装我们的Nginx, 这里使用的是最新稳定版本,

[root@master ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz 
[root@master ~]# tar -xf nginx-1.8.0.tar.gz -C /usr/local/src 
[root@master ~]# cd /usr/local/src/nginx-1.8.0 
[root@master nginx-1.8.0]# ./configure --user=nginx --group=nginx 
--with-http_ssl_module --with-http_sub_module

 

安装过程比较简单,./configure过程会报出一些依赖关系,这里一一解决之。首先,操作系统是最小化安装,并没有安装gcc,所以,第一步进行./configure的时候,就会报错。


当出现如下错误时,需要安装ssl的开发包,

./configure: error: SSL modules require the OpenSSL library. 
You can either do not enable the modules, or install the OpenSSL library 
into the system, or build the OpenSSL library statically from the source 
with nginx by using --with-openssl=<path> option.


当出现如下错误时,需要安装zlib的开发包,

./configure: error: SSL modules require the OpenSSL library. 
You can either do not enable the modules, or install the OpenSSL library 
into the system, or build the OpenSSL library statically from the source 
with nginx by using --with-openssl=<path> option.
[root@master ~]# yum install -y pcre-devel 
[root@master ~]# yum install -y gcc 
[root@master ~]# yum install -y zlib-devel 
[root@master ~]# yum install -y openssl-devel

下面来看看./configure后面几个常用的参数:

--prefix=<dir>         指定安装主目录,默认为/usr/local/nginx 
--user=<user>          指定用户身份,如果没有指定则默认使用nobody 
--group=<group>        指定组身份 
--with-http_ssl_module 启用https支持


2. Nginx的启动、重启与停止

安装完毕,我们就可以启动Nginx了,

[root@master ~]# /usr/local/nginx/sbin/nginx -c /usr/loca/nginx/conf/nginx.conf


-c是用来指定Nginx的主配置文件,如果没有指定则默认为/usr/loca/nginx/conf/nginx.conf文件。启动后,可以用ps与netstat命令查看是否启动成功,

[root@master ~]# ps -ef |grep nginx
root      1059     1  0 02:49 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     1061  1059  0 02:49 ?        00:00:00 nginx: worker process                                          
root      1063  1013  0 02:49 pts/0    00:00:00 grep nginx

[root@master ~]# netstat -antup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 192.168.1.151:8080          0.0.0.0:*                   LISTEN      1059/nginx          
tcp        0      0 192.168.1.150:8080          0.0.0.0:*                   LISTEN      1059/nginx          
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1059/nginx          
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      801/sshd            
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      877/master          
tcp        0      0 192.168.1.129:22            192.168.1.106:56004         ESTABLISHED 1009/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      801/sshd            
tcp        0      0 ::1:25                      :::*                        LISTEN      877/master          
udp        0      0 0.0.0.0:68                  0.0.0.0:*                               1007/dhclient

启动成功,我们可以访问首页去验证一下,

技术分享


3. Nginx启动脚本

Nginx并没有提供类似System V服务的管理脚本,如果我们希望开机时要让Nginx自动启动,可以执行如下命令:

[root@master ~]# echo “/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
” >> /etc/rc.local


当然,如果我们对System V的服务管理脚本情有独钟的话,可以参考如下脚本

[root@master ~]# cat /etc/init.d/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

最后,给脚本一个可执行的权限,然后使用chkconfig命令对其进行管理,

[root@master ~]# chmod 755 /etc/init.d/nginx
[root@master ~]# chkconfig nginx on


当我们对Nginx的配置文件做过一些更改后,希望在不中断当前服务的情况下,进行一个平滑的重启,可以使用如下命令,

[root@master ~]# service nginx reload

脚本中的reload函数会首先对配置文件做一个语法格式的检查,使用的是如下命令,

[root@master ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

当语法格式检查通过后,会对Nginx发出一个标记为1或者说是HUP的信号,Nginx收到后会关闭旧进程,打开新进程,如果有进程正在为一个用户提供服务,则会等待这次服务结束。

当然,我们也可以使用service nginx restart的方式去重启服务。停止Nginx,直接service nginx stop即可,或者kill掉所有的Nginx进程。


本文出自 “天道酬勤” 博客,请务必保留此出处http://lavenliu.blog.51cto.com/5060944/1663792

Nginx源代码安装

标签:nginx   源代码   编译安装   

原文地址:http://lavenliu.blog.51cto.com/5060944/1663792

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!