Tengine+Php+Mariadb 之淘宝webserver配置

下面以 纯净的centos系统为例介绍

(1) 准备前工作:

1、配置防火墙,开启80端口、3306端口
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT (允许22端口通过防火墙)
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT(允许80端口通过防火墙)
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT(允许3306端口通过防火墙)
service iptables save
service iptables start
iptables -L -n
cat /etc/sysconfig/iptables #查看完整的iptables配置文件信息
rpm -qf /etc/sysconfig/iptables-config #可以查看iptables软件包的名字和版本号(查询一个已经安装的文件属于哪个软件包)

2、关闭sellinux
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq 保存,关闭

3、更新yum源(建议使用网易的yum源)
mv CentOS6-Base-163.repo CentOS6-Base-163.repo-bak #备份系统自带的yum源
进入/etc/yum.repos.d/目录后下载网易的yum源 http://mirrors.163.com/.help/CentOS6-Base-163.repo
:%s/$releasever/6.7/g # 指定当前yum源使用的系统版本号
:wq #保存编辑过的配置文件

yum clean all #清空老的缓存
yum makecache #生成新缓存
yum update #更新yum源可升级系统内核版本
shutdown -r now #重启系统
4、 安装编译库和依赖库
yum -y install gcc gcc-c++ autoconf zlib zlib-devel

(2) 安装 tengine

安装这个软件之前,需要安装pcre 、 openssl、proxy_cache

pcre用于支持tengine伪静态
openssl安全套接层协议,可以在Internet上提供秘密性传输,对数据进行加密、安全验证、保护数据传输过程中的完整性。
proxy_cache:nginx前端web缓存,解决流量的压力。(网站架构:前端web缓存+后端web服务器) 要想使用nginx的缓存功能要保证nginx添加了proxy模块。方法是在编译nginx的时候添加。

1: 安装pcre
cd /
wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
tar -zxvf pcre-8.33.tar.gz
./configure --prefix=/usr/local/pcre
make&&make install

2: 安装 openssl
cd /
wget http://www.openssl.org/source/openssl-1.0.0a.tar.gz
tar -zxvf openssl-1.0.0a.tar.gz
./configure
make&&make install
不指定位置编译,默认安装的位置是 /usr/local/ssl

3:下载proxy_cache插件 解压下就行了,不需要安装
cd /
wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
tar zxvf ngx_cache_purge-2.1.tar.gz

3: 安装 tengine
从淘宝tengine官方下载tengine软件包,解压,编译,安装
./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module --with-http_flv_module --with-http_dav_module --with-http_gzip_static_module \
--with-http_stub_status_module --with-http_addition_module --with-openssl=/openssl-1.0.0a --with-http_ssl_module --with-pcre=/pcre-8.33 --add-module=/ngx_cache_purge-2.1
注意:编辑时候pcre 和 openssl指向的是源码包解压的路径,而不是安装的路径,否则在安装(make&&make install)的时候会提示 “make[1]: *** [/usr/local/pcre/Makefile] 127”错误。
说明了在安装nginx时候 pcre是必须指定源码路径编译的,openssl可以不是必须的。 此不能用yum方式安装pcre,我测试过,用yum安装过pcre 也提示上面的错误,原因是yum安装的pcre不能指定正确的pcre源码包的位置,这个地方是需要pcre源码包位置(pcre源码包解压路径的)。
make&&make install
/usr/local/nginx/sbin/nginx -t #返回test is successful字样说明tengine安装成功。

4:制作nginx启动脚本
vi /etc/init.d/nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/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)

sysconfig="/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/nginx"
pidfile="/usr/local/nginx/logs/nginx.pid"

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f $sysconfig ] && . $sysconfig


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 -p $pidfile $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest_q || return 6
    stop
    start
}

reload() {
    configtest_q || return 6
    echo -n $"Reloading $prog: "
    killproc -p $pidfile $prog -HUP
    echo
}

configtest() {
    $nginx -t -c $NGINX_CONF_FILE
}

configtest_q() {
    $nginx -t -q -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

# Upgrade the binary with no downtime.
upgrade() {
    local oldbin_pidfile="${pidfile}.oldbin"

    configtest_q || return 6
    echo -n $"Upgrading $prog: "
    killproc -p $pidfile $prog -USR2
    retval=$?
    sleep 1
    if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then
        killproc -p $oldbin_pidfile $prog -QUIT
        success $"$prog online upgrade"
        echo
        return 0
    else
        failure $"$prog online upgrade"
        echo
        return 1
    fi
}

# Tell nginx to reopen logs
reopen_logs() {
    configtest_q || return 6
    echo -n $"Reopening $prog logs: "
    killproc -p $pidfile $prog -USR1
    retval=$?
    echo
    return $retval
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest|reopen_logs)
        $1
        ;;
    force-reload|upgrade)
        rh_status_q || exit 7
        upgrade
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status|status_q)
        rh_$1
        ;;
    condrestart|try-restart)
        rh_status_q || exit 7
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
        exit 2
esac

保存此文件
注意需要赋予执行的权限:chmod +x /etc/init.d/nginx
chkconfig nginx on #开机自动启动

5:启动nginx服务
service nginx start
或者 /usr/local/nginx/sbin/nginx

6: 设置nginx进程用户、组、权限
groupadd nginx
useradd -s /sbin/nologin -g nginx nginx
chown nginx.nginx -R /usr/local/nginx/html
chmod 755 -R /usr/local/nginx/html

7: http://ip 访问服务器的ip ,可以看到 ”Welcome to tengine!“ 说明tengine安装成功!

(2)安装mariadb数据库

我们可以通过yum方式快速部署mysql或者mysql数据库的替代品mariadb 。 由于tengine(ningx)在编译的时候不需要php和mysql的参数信息,所有可以用yum直接快速地安装mysql或php , 整合Nginx与PHP就可以了!

由于Oracle进一步对mysql闭源的举措让人难以安心,众多互联网公司纷纷开始寻求MySQL的替代方案。而MariaDB是一个向后兼容、替代MySQL的数据库服务器。它包含所有主要的开源存储引擎。所以建议使用MariaDB 。
安装步骤如下:
配置Mariadb安装源
vi/etc/yum.repos.d/MariaDB.repo
[mariadb]
name=MariaDB
baseurl=http://yum.mariadb.org/10.0.20/centos6-x86/
gpgkey=http://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安装mariadb 数据库
yum -y install MariaDB-client MariaDB-server MariaDB-devel #注意区分大小写
service mysql start # 启动mysql数据库
mysql -u root -p 登陆mysql的时候发现mysql的密码不为空 , 所以我们破解下密码,下面介绍下 linux上mysql密码的破解方法:
service mysql stop
mysqld_safe --skip-grant-tables
然后重新开一个ssh窗口 做如下操作
mysql -u root -p 回车,此时不需要密码就能进去

1
2
3
4
5
6
7
8
9
10
11
12
13
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> update user set password=password("12332145") where user="root";
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

MariaDB [mysql]> exit

此时我们把root的密码重置为12332145了

(3) 安装php

yum -y install php # 安装php
yum -y install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mcrypt php-mbstring php-bcmath php-fpm # 安装php组件,使得php支持fastcgi模式

(4) 整合nginx与php

1: 修改nginx的配置文件/usr/local/nginx/conf/nginx.conf 把第二行注释掉,并且把nobody更改为nginx,nginx; worker_processernginx要开启的进程数 一般等于cpu的总核数 其实一般情况下开4个或8个就可 我开2个, 多了没有用,每一个nginx进程消耗10MB

2: 把71行到77行的注释去掉,并把/scripts替换为$document_root 此为PHP网站放置的路径, 如下:

1
2
3
4
5
6
7
location ~ \.php$ {
                root           html;
                 fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
               fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
                 include        fastcgi_params;
             }

3:php-fpm设置
vi /etc/php-fpm.d/www.conf
把39行和41行的apache更改为nginx

(5) 测试php效果

1:重启nginx、php-fpm服务
service nginx restart
service php-fpm start

service php-fpm restart

通过chkconfig命令将nginx mysql php-fpm 3个服务设置为开机自行启动

2:在/usr/local/nginx/html目录下创建php测试页面进行php测试
vi test.php

3: 测试
访问 http://ip/test.php 测试php效果
可以看到 php版本号 、Server API为FPM/FastCGI 等信息,说明已经可以了。

(6) 创建多站点虚拟主机

1: 在nginx主配置文件 /usr/local/nginx/conf/nginx.conf 中添加虚拟主机配置文件的包含文件
http {
server {

}
server {

}
include vhost/*.conf;
}
添加位置如上所示

2:制作站点的详情配置文件
在/usr/local/nginx/conf/下创建vhost目录,然后再在vhost目录下面创建虚拟主机站点的详情配置文件。
如我想添加一个http://ip:81 访问的站点 , 我们就创建/usr/local/nginx/conf/vhost/81.conf
vi /usr/local/nginx/conf/vhost/81.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
server
      {
         listen   81;
         server_name localhost;
         index    index.php  index.html  index.htm default.html default.htm default.php;
         root /www;
 location ~ \.php$ {
            root           /www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
            include        fastcgi_params;
        }
location /status
{
    stub_status on;
    access_log off;
    auth_basic "status";
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
       {
               expires 30d;
       }
location ~ .*\.(js|css)?$
       {
            expires   12h;
       }
access_log off;
}

3: 创建站点的存放目录并且给予访问权限
mkdir /www
chown nginx.nginx -R /www
chmod 755 -R /www

相关新闻

联系我们

全国服务热线

400-033-9553

电子邮件:admin@example.com
工作时间:09:00-17:00 周一至周五

在线客服
关注微信
关注微信
分享本页
返回顶部