声明:本系列教程由马哥教育提供指导:

Centos6.7编译安装Nginx1.81 +mysql-5.5.33 + php-5.5

1nginx简介:

Nginx是一款轻量级Web 服务器/反向代理服务器及电子邮件IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度新浪网易腾讯等。

Nginx的特性:

              模块化设计、较好扩展性

              高可靠性

                     master-->worker

              低内存消耗

                     10000个keep-alive连接在Nginx仅消耗2.5MB

              支持热部署

                     不停机而更新配置文件、更换日志文件、更新服务器程序版本

             

基本功能:

              静态资源的web服务器,能缓存打开的文件 描述符

              http,smtp, pop3协议的反向代理服务器,缓存、负载均衡;

              支持FastCGI (fpm)

              模块化,非DSO机制,过滤器zip,SSI及图像大小调整;

              支持SSL

             

扩展功能:

              基于名称和IP的虚拟主机;

              支持keepalive

              支持平滑升级

              定制访问日志,支持使用日志缓冲区提高日志存储性能

              支持url rewrite

              支持路径别名

              支持基于IP及用户的访问控制

              支持速率限制,支持并发数限制

             

Nginx的基本架构:

              一个master进程,生成一个或多个worker

              事件驱动: epoll, kqueue, /dev/poll (event ports)

                     消息通知:select, poll, rt signals

              支持sendfile, sendfile64

              支持AIO

              支持mmap

Nginx的版本

    Nginx版本分为主线版和稳定版,主线版更新速度较快,从官网上看大约一个月更新1-2次,目前 最新主线版已更新到nginx-1.9.14,而官方宣布的最新稳定版则是nginx-1.8.1,and本文就以1.8.1版为例演示其在CentOS7上的安装和配置过程。Nginx官方网站http://nginx.org/。

2、下载:

[root@nginx ~]# mkdir /tools
[root@nginx ~]# cd /tools/
[root@nginx tools]# wget http://nginx.org/download/nginx-1.8.1.tar.gz
[root@nginxtools]# ls nginx-1.8.1.tar.gz -sh
816K nginx-1.8.1.tar.gz
# 可以看到nginx源码包非常小

3、解压

[root@nginx tools]# tar xf nginx-1.8.1.tar.gz
[root@nginx tools]# cd nginx-1.8.1
[root@nginx nginx-1.8.1]# ls
auto  CHANGES CHANGES.ru  conf  configure contrib  html  LICENSE man  README  src

4、安装准备

4.1、先配置阿里云yum源:

1、备份
# mv/etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
 
2、下载新的CentOS-Base.repo 到/etc/yum.repos.d/
CentOS 6:
# wget-O /etc/yum.repos.d/CentOS-Base.repo 

CentOS 7:
# wget-O /etc/yum.repos.d/CentOS-Base.repo 
 
3、之后运行yummakecache生成缓存

4.2、添加Epel源:

1、备份(如有配置其他epel源)
# mv/etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
# mv/etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
 
2、下载新repo 到/etc/yum.repos.d/
epel(RHEL 7):
    # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
            
epel(RHEL 6):
    # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

4.3、解决依赖关系

编译安装nginx需要事先需要安装开发包组"DevelopmentTools" "Development Libraries"。同时,还需要专门安装pcre-developenssl-devel包:

[root@nginx nginx-1.8.1]# yum grouplist"Development Tools" "Development Libraries"
[root@nginxnginx-1.8.1]# yum groupinstall Development Tools Development Libraries -y
# yum安装nginx必须的依赖库 
# yum -y install pcre-devel openssl openssl-devellibxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed

4.4、创建用户:

创建一个名为nginx且没有登录权限的用户和一个名为nginx的用户组,用来运行nginx服务进程:

[root@nginx nginx-1.8.1]# groupadd -r nginx
[root@nginx nginx-1.8.1]# useradd -r -g nginx nginx -s/sbin/nologin -M              # g GID:指明用户所属基本组,可为组名,也可以GID;
                     #     -d /PATH/TO/HOME_DIR: 以指定的路径为家目录;
                     #     -s SHELL: 指明用户的默认shell程序,可用列表在/etc/shells文件中;
                     #     -r: 创建系统用户
            #  -M: 不自动创建用户的家目录
[root@nginx nginx-1.8.1]# tail -1/etc/passwd
nginx:x:500:500::/home/nginx:/sbin/nologin
[root@nginx nginx-1.8.1]# id nginx
uid=497(nginx)gid=497(nginx) groups=497(nginx)

4.5、新建Nginx安装时所需要的目录

[root@nginx ~]# cd /var/tmp/
[root@nginx tmp]# mkdir -p/var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
[root@nginx tmp]# mkdir -p /var/run/nginx
[root@nginx tmp]# cd /tools/nginx-1.8.1
[root@nginxnginx-1.8.1]#

5、编译安装

# ./configure \
  --prefix=/usr/local/nginx \
 --sbin-path=/usr/local/nginx/sbin/nginx \
 --conf-path=/etc/nginx/nginx.conf \
 --error-log-path=/var/log/nginx/error.log \
 --http-log-path=/var/log/nginx/access.log \
 --pid-path=/var/run/nginx/nginx.pid \
 --lock-path=/var/lock/nginx.lock \
 --user=nginx \
 --group=nginx \
 --with-http_ssl_module \
 --with-http_perl_module \
  --with-http_flv_module\
 --with-http_stub_status_module \
 --with-http_gzip_static_module \
 --http-client-body-temp-path=/var/tmp/nginx/client/ \
 --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
 --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
 --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
 --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre
# make && make install

6、为nginx提供SysV init脚本:

 

新建文件/etc/rc.d/init.d/nginx,内容如下:

[root@nginx ~]# vim /etc/rc.d/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的sbin目录
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/etc/nginx/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' -`
  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 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

7、给脚本赋予执行权限:

[root@nginx ~]# chmod +x /etc/rc.d/init.d/nginx

8、添加至服务管理列表,并让其开机自动启动:

[root@nginx ~]# chkconfig --add nginx
[root@nginx~]# chkconfig nginx on

9、启动服务

[root@nginx nginx-1.8.1]# service nginx start

10、防火墙设置:

(1) 插入新的防火墙规则,开通80端口

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

(2) 保存规则:

/etc/rc.d/init.d/iptables save

(3) 重启防火墙服务

/etc/init.d/iptables restart

注意:内网环境中iptables建议关闭:
# server iptables stop
# chkonfig iptables off


11关闭SELINUX

永久关闭:
[root@nginx nginx-1.8.1]# vi/etc/selinux/config
# This file controls the state ofSELinux on the system.
# SELINUX= can take one of thesethree values:
#       enforcing - SELinux security policy isenforced.
#       permissive - SELinux prints warningsinstead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy inuse. Possible values are:
#       targeted - Only targeted network daemonsare protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted

# SELINUXTYPE= type of policy inuse. Possible values are:
#       targeted - Only targeted network daemonsare protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted

临时关闭:

[root@nginx nginx-1.8.1]# setenforce 0

12、测试:

浏览器访问http://192.168.0.111

wKiom1ch5UjCwa1MAAEGz59h-EU956.png

nginx编译安装成功!