ubuntu环境下nginx源码编译安装

1、更新系统

sudo apt-get update && sudo apt-get upgrade

2、安装nginx的依赖包 zlib pcre openssl(可以源码安装也可以直接系统安装)

sudo apt-get install libpcre3 libpcre3-dev zlib1g-dev libssl-dev build-essential

3、下载openssl源码包

wget http://www.openssl.org/source/openssl-1.0.2a.tar.gz

sudo tar -zxvf openssl-1.0.2a.tar.gz -C /usr/local/src/

cd /usr/local/src/openssl-1.0.2a/

sudo ./config

sudo make && sudo make install

4、下载nginx源码包

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

sudo tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/src/

cd /usr/local/src/nginx-1.8.0

sudo ./configure --prefix=/usr/local/nginx --with-openssl=/usr/include/openssl

sudo make && sudo make install

5、配置nginx 开机服务。

默认这么安装好以后每次检查配置、重启之类的操作略麻烦,所以我们模仿 Ubuntu 14.04 官方源,给系统设置个 nginx 服务,方便我们检查配置、启动重启关闭 Nginx 以及开机自动启动 Nginx

sudo vim /etc/init.d/nginx

插入如下内容:

  1 #!/bin/sh
  2 
  3 ### BEGIN INIT INFO
  4 # Provides:          nginx
  5 # Required-Start:    $local_fs $remote_fs $network $syslog
  6 # Required-Stop:     $local_fs $remote_fs $network $syslog
  7 # Default-Start:     2 3 4 5
  8 # Default-Stop:      0 1 6
  9 # Short-Description: starts the nginx web server
 10 # Description:       starts nginx using start-stop-daemon
 11 ### END INIT INFO
 12 
 13 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 14 DAEMON=/usr/sbin/nginx
 15 NAME=nginx
 16 DESC=nginx
 17 
 18 # Include nginx defaults if available
 19 if [ -f /etc/default/nginx ]; then
 20    . /etc/default/nginx
 21 fi
 22 
 23 test -x $DAEMON || exit 0
 24 
 25 set -e
 26 
 27 . /lib/lsb/init-functions
 28 
 29 test_nginx_config() {
 30    if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
 31       return 0
 32    else
 33       $DAEMON -t $DAEMON_OPTS
 34       return $?
 35    fi
 36 }
 37 
 38 case "$1" in
 39    start)
 40       echo -n "Starting $DESC: "
 41       test_nginx_config
 42       # Check if the ULIMIT is set in /etc/default/nginx
 43       if [ -n "$ULIMIT" ]; then
 44          # Set the ulimits
 45          ulimit $ULIMIT
 46       fi
 47       start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
 48           --exec $DAEMON -- $DAEMON_OPTS || true
 49       echo "$NAME."
 50       ;;
 51 
 52    stop)
 53       echo -n "Stopping $DESC: "
 54       start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
 55           --exec $DAEMON || true
 56       echo "$NAME."
 57       ;;
 58 
 59    restart|force-reload)
 60       echo -n "Restarting $DESC: "
 61       start-stop-daemon --stop --quiet --pidfile \
 62           /var/run/$NAME.pid --exec $DAEMON || true
 63       sleep 1
 64       test_nginx_config
 65       # Check if the ULIMIT is set in /etc/default/nginx
 66       if [ -n "$ULIMIT" ]; then
 67          # Set the ulimits
 68          ulimit $ULIMIT
 69       fi
 70       start-stop-daemon --start --quiet --pidfile \
 71           /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
 72       echo "$NAME."
 73       ;;
 74 
 75    reload)
 76       echo -n "Reloading $DESC configuration: "
 77       test_nginx_config
 78       start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
 79           --exec $DAEMON || true
 80       echo "$NAME."
 81       ;;
 82 
 83    configtest|testconfig)
 84       echo -n "Testing $DESC configuration: "
 85       if test_nginx_config; then
 86          echo "$NAME."
 87       else
 88          exit $?
 89       fi
 90       ;;
 91 
 92    status)
 93       status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
 94       ;;
 95    *)
 96       echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
 97       exit 1
 98       ;;
 99 esac
100 
101 exit 0

注意要设置好nginx的启动路径 DAEMON=/usr/sbin/nginx

6、设置文件权限并增加到系统服务

sudo chmod +x ./nginx

sudo update-rc.d nginx defaults

7、启动nginx

sudo /etc/init.d/nginx