lamp

配置LAMP–fastcgi模式

因为CentOS6上PHP-5.3.2默认不支持fpm机制,并且httpd-2.2默认不支持fcgi协议,所以此模式只在CentOS7上实验,CentOS6.想实现需要编译安装。

一、结构图

Markdown

用三台虚拟机来模拟服务器:

172.16.2.10--http服务器
172.16.2.20--php-fpm服务器
172.16.2.30--mariadb服务器

二、配置

1.配置mariadb

在2.30的主机上安装mariadb

~]# yum install mariadb-server

修改配置文件

1
2
3
4
5
[root@localhost ~]# vim /etc/my.cnf.d/server.cnf
# this is read by the standalone daemon and embedded servers
[server] #在server下添加
skip_name_resolve=ON #跳过名称解析
innodb_file_per_table=ON #将共享表空间改为独立表空间

启动服务

~]# systemctl start mariadb.service

创建数据库并重启服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database testdb; #创建数据库
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> exit
Bye
[root@localhost ~]# systemctl restart mariadb.service
[root@localhost ~]#

2.配置php-fpm
在2.20主机上安装php-fpm以及php与mariadb的驱动php-mysql

~]# yum install -y php-fpm php-mysql

修改配置文件

需要修改:
    listen = 172.16.2.20:9000           #监听本机IP和端口,这里填本机IP
    listen.allowed_clients = 172.26.2.10        #允许那些客户端连接这里填httpd的IP
取消注释:
    pm.status_path = /status        #查看状态信息

    ping.path = /ping               #查看网络信息
    ping.response = pong

    chdir = /var/www                #网页文件存放目录

    php_value[session.save_path] = /var/lib/php/session 
    #PHP的session的存放路径,默认没有,需要手动创建,并改属主属组,为apache

创建所需文件并改属主属组

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# mkdir -pv /var/www/html
mkdir: created directory ‘/var/www’
mkdir: created directory ‘/var/www/html’
[root@localhost ~]# chown -R apache.apache /var/www/
[root@localhost ~]#
[root@localhost ~]# mkdir -pv /var/lib/php/session
mkdir: created directory ‘/var/lib/php/session’
[root@localhost ~]# chown -R apache.apache /var/lib/php/session
[root@localhost ~]#

启动服务

~]# systemctl start php-fpm

3.配置httpd

在2.10主机上安装httpd

~]# yum install httpd

修改配置文件

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName 172.16.2.10:80 #这里改为本机IP,因为httpd在本台主机上

创建虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# vim /etc/httpd/conf.d/virtual.conf
DirectoryIndex index.php
<VirtualHost *:80>
ServerName 172.16.2.10 #服务器名写httpd的IP
DocumentRoot "/var/www/html/"
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.2.20:9000/var/www/html/$1 #这里写php-fpm主机的IP
<Directory "/var/www/html/">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

在php-fpm主机上创建php测试页并重启服务

1
2
3
4
5
6
7
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.php
<?php
phpinfo();
?>
[root@localhost html]# systemctl restart php-fpm

浏览器输入httpd的ip:

Markdown

页面成功显示

表示httpd与php-fpm之间连接通信正常

三、配置PhpMyAdmin

1.在php-fpm和httpd主机上,配置phpmyadmin。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# unzip phpMyAdmin-4.0.10.20-all-languages.zip
[root@localhost ~]# ll
total 7292
-rw-------. 1 root root 2642 Jun 6 2017 anaconda-ks.cfg
drwxr-xr-x 9 root root 4096 Mar 28 21:03 phpMyAdmin-4.0.10.20-all-languages
-rw-r--r-- 1 root root 7457007 Jun 5 22:03 phpMyAdmin-4.0.10.20-all-languages.zip
把解压后的文件改名后放到/var/www/html/目录下
[root@localhost ~]# mv phpMyAdmin-4.0.10.20-all-languages pma
[root@localhost ~]# ll
total 7292
-rw-------. 1 root root 2642 Jun 6 2017 anaconda-ks.cfg
-rw-r--r-- 1 root root 7457007 Jun 5 22:03 phpMyAdmin-4.0.10.20-all-languages.zip
drwxr-xr-x 9 root root 4096 Mar 28 21:03 pma
[root@localhost ~]# mv pma /var/www/html/
[root@localhost ~]#

修改pma的属主数组

1
2
3
4
5
6
[root@localhost html]# chown -R apache.apache pma
[root@localhost html]# ll
total 8
-rw-r--r-- 1 root root 22 Jun 5 23:25 index.php
drwxr-xr-x 9 apache apache 4096 Mar 28 21:03 pma
[root@localhost html]#

修改配置文件

1
2
3
4
5
6
7
8
9
10
[root@localhost pma]# mv config.sample.inc.php config.inc.php
[root@localhost pma]# vim config.inc.php
修改
cfg['Servers'][$i]['host'] = '172.16.2.30'; #这里改为mariadb的IP
cfg['blowfish_secret'] = 'qweqweqwehh8b7c6d'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */ #在第一个选项后输入即为随机字符,不能输入'/'
[root@localhost pma]# vim libraries/config.default.php
修改
cfg['Servers'][$i]['host'] = '172.16.2.30'; #这里改为mariadb的IP

四、在mariadb上创建用户授权;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.16.2.10' IDENTIFIED BY '111111';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.16.2.20' IDENTIFIED BY '111111';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.16.2.30' IDENTIFIED BY '111111';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> exit
Bye
[root@localhost ~]#

五、重启所有主机服务并测试;

httpd主机

[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# 

php-fpm主机

[root@localhost ~]# systemctl restart php-fpm
[root@localhost ~]#

mariadb主机

[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# 

打开网页输入172.16.2.10/pma/index.php

Markdown
Markdown

成功。

<% if (theme.canvas_nest) { %> <% } %> s