代码改变世界

linux 源码安装 Nginx

2015-09-15 15:48  abce  阅读(459)  评论(0编辑  收藏  举报

1.安装前环境准备
安装make:
# yum -y install gcc automake autoconf libtool make
安装g++:
# yum install gcc gcc-c++

2.一般都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩

安装pcre
下载地址: http://www.pcre.org/
# tar -xvf pcre-8.36.tar.gz
# cd pcre-8.36
# ./configure --prefix=/usr/local/pcre
# make
# make install

安装zlib
下载地址: http://www.zlib.net/
# cd /usr/local/src
# tar -xvf zlib-1.2.8.tar.gz
# cd zlib-1.2.8
# ./configure --prefix=/usr/local/zlib
# make
# make install

安装ssl(某些vps默认没装ssl)
# cd /usr/local/src
# wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
# tar -zxvf openssl-1.0.1c.tar.gz

3.安装Nginx
# groupadd vc_php_grp
# useradd -g vc_php_grp vc_php_usr -s /bin/nologin

./configure --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--user=vc_php_usr \
--group=vc_php_grp \
--with-http_gzip_static_module \
--with-zlib=/usr/local/src/zlib-1.2.8 \
--with-pcre=/usr/local/src/pcre-8.36 \
--with-openssl=/usr/local/src/openssl-1.0.1c
Configuration summary
  + using PCRE library: /usr/local/src/pcre-8.36
  + using OpenSSL library: /usr/local/src/openssl-1.0.1c
  + using builtin md5 code
  + sha1 library is not found
  + using zlib library: /usr/local/src/zlib-1.2.8

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  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"

注意:
--with-openssl=/usr/local/src/openssl-1.0.1c
--with-zlib=/usr/local/src/zlib-1.2.8
--with-pcre=/usr/local/src/pcre-8.36
指向的是源码包解压的路径,而不是安装的路径,否则会报错

4.启动nginx

/usr/local/nginx/sbin/nginx

5.设置开机重启

vi /etc/init.d/nginx  (输入下面的代码)
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL 

# chmod 775 /etc/init.d/nginx #赋予文件执行权限
# chkconfig nginx on #设置开机启动
# /etc/init.d/nginx restart #重启

在浏览器中打开服务器IP地址,测试Nginx安装是否成功。

如果你需要处理php脚本的话,还需要安装php-fpm