搭建LAMP

    xiaoxiao2025-04-26  20

    LAMP

    安装要求1安装环境安装过程安装httpd安装php模块安装mariadb为www1上安装wordpress为www2上提供php-admin为www2虚拟机提供https 安装要求2安装环境安装过程安装mariadb安装php-fpm安装httpd测试三台主机是否成功通信

    安装要求1

    CentOS 7, lamp (module); 要求:(1) 三者分离于两台主机; (2) 一个虚拟主机用于提供phpMyAdmin;另一个虚拟主机用于提供wordpress; (3) xcache (4) 为phpMyAdmin提供https虚拟主机;

    安装环境

    centos版本:7.4httpd版本:2.4php版本:5.4mariadb版本:5.5两台主机:11.2.2.189上提供mariadb , 11.2.2.172上提供php-module和httpd

    安装过程

    安装httpd

    (1)在172上安装httpd

    [root@localhost ~]# yum install httpd -y

    (2)修改配置文件 (3)配置两台虚拟机www1和www2

    #www1虚拟机 [root@localhost conf.d]# cat www1.conf <VirtualHost *:80> ServerName www1.ydong.com DocumentRoot '/web/vhosts/www1' <Directory '/web/vhosts/www1> Options None Allowoverride None Require all granted </Directory> </VirtualHost> #www2虚拟机 [root@localhost conf.d]# cat www2.conf <VirtualHost *:80> ServerName www2.ydong.com DocumentRoot '/web/vhosts/www2' <Directory '/web/vhosts/www2> Options None Allowoverride None Require all granted </Directory> </VirtualHost>

    安装php模块

    (1)在172上安装php,php-mysql(提供连接数据的服务)

    [root@localhost ~]# yum install -y php php-mysql php-mbstring

    (2)测试php是否能正常使用

    #使用php内置函数查看,网页显示php信息即可 [root@localhost vhosts]# cat index.php <?php phpinfo(); ?> [root@localhost vhosts]# pwd /web/vhosts #httpd网页存放路径

    安装mariadb

    (1)在189主机上安装mariadb

    [root@localhost ~]# yum install -y mariadb-server #启动mariadb服务 [root@localhost ~]# systemctl start mariadb.service

    (2)创建一个wordpress数据库,然后向172主机上进行授权访问该数据库

    MariaDB [(none)]> CREATE DATABASE wordpress; MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | wordpress | +--------------------+

    (2)向172主机授权访问wordpress数据库

    MariaDB [(none)]> GRANT ALL ON wordpress.* TO 'myuser'@'11.2.2.172' IDENTIFIED BY 'mypass'; Query OK, 0 rows affected (0.00 sec)

    (3)在172主机上测试是否可以连接189的数据库

    root@localhost conf.d]# mysql -umyuser -h11.2.2.189 -pmypass Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 5.5.60-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> SHOW DATABASE; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DATABASE' at line 1 MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | wordpress | +--------------------+ 2 rows in set (0.00 sec) MariaDB [(none)]> QUIT Bye

    补充:记录下遇到的问题,在远程登录时发现ERROR 2003 (HY000): Can't connect to MySQL server on '11.2.2.189' (113),这是因为189上禁止了远程登录功能,需要在mysql.user表下需要将localhost的选项改为%。

    MariaDB [mysql]> SELECT user,host FROM user; +--------+-----------------------+ | user | host | +--------+-----------------------+ | root | % | #原来此处为localhost,使用MySQL> update user set host='%' where user = 'root'即可,需要手动FLUSH PRIVILEGES | mysql | 11.2.2.% | | myuser | 11.2.2.172 | | root | 127.0.0.1 | | root | ::1 | | root | localhost.localdomain | +--------+-----------------------+ 6 rows in set (0.00 sec)

    为www1上安装wordpress

    (1)将wordpress目录放置到/web/vhosts/www1下,并修改配置文件wp-config.php (2)访问www1.ydong.com/wordpress (3)填完信息后,重新刷新即可

    为www2上提供php-admin

    (1)将php-admin目录放置到/etc/vhosts/www2下,并修改配置文件php-admin/config.inc.php (2)访问www2.ydong.com/php-admin (3)安装php加速软件:php-xcache

    [root@localhost vhosts]# yum install -y php-xcache-3.1.0-2.el7.remi.5.4.x86_64.rpm

    (4)重启httpd服务。xcache加载成功

    为www2虚拟机提供https

    由于重新换了网络环境,所以为php-admin又重新配置了一下虚拟机

    [root@www2 conf.d]# cat phpadmin.conf <VirtualHost *:80> ServerName www.phpadmin.com DocumentRoot "/web/vhosts/www2/php-admin" <Directory '/web/vhosts/www2/php-admin> Options None Allowoverride None Require all granted </Directory> </VirtualHost>

    (1)安装mod_ssl模块

    [root@www2 conf.d]# yum install -y mod_ssl

    (2)我的CA主机在上边的数据库上,CA自签证书就不写了。点击这里可以看过程,这里只写httpd的请求生成

    [root@www2 ssl]# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr -days 30 You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:BeiJing Locality Name (eg, city) [Default City]:BeiJing Organization Name (eg, company) [Default Company Ltd]:magedu Organizational Unit Name (eg, section) []:ops Common Name (eg, your name or your server's hostname) []:www.phpadmin.com Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:

    (3)发给CA主机签署请求

    [root@localhost ~]# openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 30 Using configuration from /etc/pki/tls/openssl.cnf Check that the request matches the signature Signature ok Certificate Details: Serial Number: 5 (0x5) Validity Not Before: May 28 13:23:36 2019 GMT Not After : Jun 27 13:23:36 2019 GMT Subject: countryName = CN stateOrProvinceName = BeiJing organizationName = magedu organizationalUnitName = ops commonName = www.phpadmin.com X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 98:00:BB:8B:44:78:7E:8B:C7:FB:53:10:C8:5A:D7:39:82:CE:11:34 X509v3 Authority Key Identifier: keyid:15:56:D7:CC:A6:6D:02:D6:29:18:C5:14:70:18:88:85:16:49:BD:FC Certificate is to be certified until Jun 27 13:23:36 2019 GMT (30 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated

    (4)修改配置文件/etc/httpd/conf.d/ssl.conf, 把CA自签证书导入到浏览器中,并且把CA主机签署成功的文件放置到/etc/httpd/ssl下 (6)重启httpd服务,访问www.phpadmin.com

    安装要求2

    CentOS 7, lamp (php-fpm); 要求:(1) 三者分离于三台主机; (2) 一个虚拟主机用于提供phpMyAdmin;另一个虚拟主机用于提供wordpress; (3) xcache

    安装环境

    Centos版本:7.2httpd版本:2.4php-fpm:5.4mariadb:5.6centos主机:192.168.199.213提供mariadb,116提供httpd ,161提供php-fpm

    安装过程

    安装mariadb

    (1)安装mariadb

    [root@bogon ~]# yum install -y mariadb-server

    (2)运行服务并在启动后运行mysql_secure_installation (3)修改my.cnf的配置文件 (4)向php-fpm进行授权

    安装php-fpm

    前言:在安装之前首先需要查看httpd是否含有fcgi模块。 (1)安装php-fpm php-mysql php-mbstring三个软件

    [root@bogon ~]# yum install -y php-fpm php-mbstring php-mysql

    (2)php-fpm配置文件:/etc/php-fpm.d/www.conf

    稍后进行测试

    安装httpd

    (1)安装httpd服务

    yum install -y httpd

    (2)进行httpd.conf设置 (3)测试httpd服务是否正常启动,创建/web/vhosts/test.html (4)配置httpd反向代理,在/etc/httpd/conf.d/fstcgi.conf,添加如下几行

    1 DirectoryIndex index.php #支持index.php网页 2 ProxyRequests Off #关闭正向代理 3 ProxyPassMatch ^/(.*\.php)$ fcgi://193.168.199.161:9000/web/vhosts/$1 #将php文件的处理转发至php-fpm主机上

    测试三台主机是否成功通信

    注释:此处测试出现了很多问题,经过一轮轮的测试终于完成。在测试前,关闭防火墙以及selinux。 (1)在php-fpm主机上创建/web/vhosts目录,也就是跟httpd.conf里存放网页的路径一样。并在/web/vhosts目录下创建index.php(用来检测是否连接数据库)以及test.php(用来测试是否连接httpd)

    [root@bogon vhosts]# cat test.php <?php phpinfo(); ?> [root@bogon vhosts]# cat index.php <?php $conn = mysql_connect('192.168.199.213','myuser','mypass'); if($conn) echo "ok"; else echo "fxxk"; ?>

    (2)登录网页测试 为两台虚拟主机安装wordpress和php-admin,两个软件配置方式与安装要求1里的配置一模一样,只不过ip地址换下。这里就不在写了 (3)为www1.ydong.com 安装wordpress,wordpress下载好解压放到php-fpm主机上的/web/vhosts即可。之前创建的fstcgi.conf删除即可。因为之前只是为了测试来用的。

    [root@bogon conf.d]# cat www1.conf DirectoryIndex index.php <VirtualHost *:80> ServerName www1.ydong.com DocumentRoot "/web/vhosts/wordpress" ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.199.161:9000/web/vhosts/wordpress/$1 <Directory "/web/vhosts/wordpress"> Options None Allowoverride None Require all granted </Directory> </VirtualHost>

    (2)登录网站测试 (3)为www2.ydong.com提供php-admin

    [root@bogon conf.d]# cat www2.conf DirectoryIndex index.php <VirtualHost *:80> ServerName www2.ydong.com DocumentRoot "/web/vhosts/phpadmin" ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.199.161:9000/web/vhosts/phpadmin/$1 <Directory "/web/vhosts> Options None Allowoverride None Require all granted </Directory> </VirtualHost>

    (4)登录网页测试

    最后mark一下:期间遇到了很多问题,浏览路径找不到文件,原来在httpd主机上创建的目录要跟php-fpm上的目录一致。创建完成之后,再次访问就正常了。配置虚拟机时没有删除fstcgi.conf文件,访问网页不成功,删除fstcgi.conf之后,恢复正常。应该是fstcgi和虚拟机主机的配置有冲突。

    最新回复(0)