nginx-1.11.10 在Linux服务器下的安装完整步骤

时间:2022-09-01 15:25:53

基于Linux服务器下nginx源码的安装配置流程

1、下载源码

官网地址http://nginx.org/en/download.html

下载完以后上传到服务器

或者服务器可以上外网的话直接用下面的命令下载

wget http://nginx.org/download/nginx-1.11.10.tar.gz

2、解压

tar -zxvf nginx-1.11.10.tar.gz

3、进入解压缩后的目录执行配置文件文件

[root@mysql ~]# cd nginx-1.11.10
[root@mysql nginx-1.11.10]# ./configure 
注意此时一些编译包未安装的话可能出现以下报错

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

4、安装必要的依赖包

一般编译nginx的 话需要安装以下的包,也可根据报错信息来安装

gcc,openssl,openssl-devel,zlib,pcre-devel,pcre,使用yum安装这些包,这里以pcre-devel 为例

[root@mysql nginx-1.11.10]# yum install pcre-devel
已加载插件:refresh-packagekit, security, ulninfo
设置安装进程
解决依赖关系
--> 执行事务检查
---> Package pcre-devel.x86_64 0:7.8-7.el6 will be 安装
--> 完成依赖关系计算

依赖关系解决

================================================================================
 软件包               架构             版本                仓库            大小
================================================================================
正在安装:
 pcre-devel           x86_64           7.8-7.el6           base           320 k

事务概要
================================================================================
Install       1 Package(s)

总下载量:320 k
Installed size: 957 k
确定吗?[y/N]:y
下载软件包:
pcre-devel-7.8-7.el6.x86_64.rpm                          | 320 kB     00:00     
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
 Userid: "CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>"
 From  : http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
确定吗?[y/N]:y
运行 rpm_check_debug 
执行事务测试
事务测试成功
执行事务
  正在安装   : pcre-devel-7.8-7.el6.x86_64                                  1/1 
  Verifying  : pcre-devel-7.8-7.el6.x86_64                                  1/1 

已安装:
  pcre-devel.x86_64 0:7.8-7.el6                                                 

完毕!

5、重新执行配置文件

[root@mysql nginx-1.11.10]# ./configure 

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

6、编译源文件

[root@mysql nginx-1.11.10]# make

7、安装

[root@mysql nginx-1.11.10]# make install

8、检查是否正常安装

[root@mysql nginx-1.11.10]# whereis nginx
nginx: /usr/local/nginx

9、启动nginx

[root@mysql nginx-1.11.10]# /usr/local/nginx/sbin/nginx
[root@mysql nginx-1.11.10]# ps -ef|grep -i nginx
root      4503     1  0 16:22 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    4504  4503  0 16:22 ?        00:00:00 nginx: worker process      
root      4509  5266  0 16:23 pts/1    00:00:00 grep -i nginx


10、如果作为服务器一般是开机启动,需要作如下操作将nginx注册为系统任务

[root@mysql nginx-1.11.10]# vim /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:      /usr/local/nginx/conf/nginx.conf 
# 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
 
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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 3 
    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


11、配置自动启动

[root@mysql nginx-1.11.10]# chkconfig --add nginx
[root@mysql nginx-1.11.10]# chkconfig --list nginx
nginx          	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
[root@mysql nginx-1.11.10]# chkconfig --level 2345 nginx on
[root@mysql nginx-1.11.10]# chkconfig --list nginx
nginx          	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭

至此在linux下的nginx 完全安装完毕,如果不想注册为系统服务,10-11可忽略。

在ubantu下比较方便 apt-get nginx即可