LAMP是什么?LAMP企业中最常用的服务,也是非常稳定的网站架构平台。其中L-指的是Linux,A-指的是Apache,m-指的是mysql或者marriDB,p-php。相信大家对这些都已经非常熟悉了,但是对于刚接触的新手来说,可能还不是太明白,要装什么包啊,什么模块啊。其实LAMP并不难,下面就和大家分享一下以rpm包的方式安装LAMP,配置基于域名的虚拟机主机并部署PhpMyAdmin 。1 安装httpd并配置基于域名的虚拟主机1.1安装
代码语言:javascript复制yum install httpd
systemctl start httpd.service
1.2 配置基于域名的虚拟机主机实现(1) 关闭防火墙和SELINUX
代码语言:javascript复制systemctl stop firewalld
systemctl disable firewalld
(2)建立虚拟主机的根目录和文件
代码语言:javascript复制mkdir /data/{a,b,c}site
echo asite > /data/asite/index.html
echo bsite > /data/bsite/index.html
echo csite > /data/csite/index.html
(3)创建虚拟主机的配置文件
代码语言:javascript复制vim /etc/httpd/conf.d/virtualhost.conf
<virtualhost *:80>
DocumentRoot "/data/asite"
servername www.a.com
<directory /data/asite>
Require all granted
</directory>
customlog /var/log/httpd/access_a.log testlog
</virtualhost>
<virtualhost *:80>
servername www.b.net
DocumentRoot "/data/bsite"
<directory /data/bsite>
Require all granted
</directory>
customlog /var/log/httpd/access_b.log testlog
</virtualhost>
<virtualhost *:80>
servername www.c.com
DocumentRoot "/data/csite"
<directory /data/csite>
Require all granted
</directory>
customlog /var/log/httpd/access_c.log testlog
</virtualhost>
(4)重启Apache
代码语言:javascript复制systemctl restart httpd.service
(5)利用宿主机Windows访问虚拟机前先修改Windows的hosts文件修改C:WindowsSystem32driversetchosts根据虚拟机的地址添加域名解析,192.168.52.128是自己httpd服务器的地址。
代码语言:javascript复制192.168.52.128 www.a.com
192.168.52.128 www.b.net
192.168.52.128 www.c.com
(6)在Windows访问三个站点打开浏览器分别输入三个地址:
<img src="https://www.magedu.com/wp-content/uploads/2020/06/68027b8841d84fa883a4585648074674.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
<img src="https://www.magedu.com/wp-content/uploads/2020/06/1941928e78414b81aaf68b5e63125e07.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
<img src="https://www.magedu.com/wp-content/uploads/2020/06/abe69c3c789a4109899216ba53725e75.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
2. 安装mariadb(1)yum安装
代码语言:javascript复制yum install mariadb-server
systemctl start mariadb
(2)运行安全脚本
代码语言:javascript复制/usr/bin/mysql_secure_installation
运行此脚本,执行:设置数据库管理员root口令禁止root远程登录删除anonymous用户帐号删除test数据库(3)配置mariadb
代码语言:javascript复制vim /etc/my.cnf
[mysqld]
innodb_file_per_table = ON
skip_name_resolve = ON
(4)重启mariadb
代码语言:javascript复制systemctl restart mariadb
3 安装php(1)yum安装
代码语言:javascript复制yum -y install php php-mysql
安装后重启httpd服务:
代码语言:javascript复制systemctl restart httpd.service
(2)编写php脚本,看一下是否能解析成功:vim /var/www/html/testphp.php
<img src="https://www.magedu.com/wp-content/uploads/2020/06/a38f4dff3c274412b828a79be54a66d7.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
效果:
<img src="https://www.magedu.com/wp-content/uploads/2020/06/0ef3ef28b31b42ef800504dc169b3fda.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
(3)配置php通过PDO方式连接数据库
代码语言:javascript复制cp /usr/share/doc/php-common-5.4.16/php.ini-development /etc/php.ini
编辑此文件,加入这一行:
代码语言:javascript复制vim /etc/php.ini
extention=pdo.so
重启服务:
代码语言:javascript复制systemctl restart httpd.service
(4)编写pdo测试代码:
代码语言:javascript复制vim /data/asite/pdo.php
<?php
try {
$user='root';
$pass='magedu';
$dbh = new PDO('mysql:host=192.168.34.27;dbname=mysql', $user, $pass);
foreach($dbh->query('SELECT user,host from user') as $row) {
print_r($row);
}$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
愉快地实现了pdo连接mysql,上面测试代码的实现结果:
<img src="https://www.magedu.com/wp-content/uploads/2020/06/b303b084724e4535ba067e45bc1fa5e3.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
4. 部署phpMyAdmin(1)下载phpMyAdmin,放在asite下面:
<img src="https://www.magedu.com/wp-content/uploads/2020/06/66112e1a6fe64a67998a94c6824826db.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
(2)解压缩
代码语言:javascript复制tar xvf phpMyAdmin-4.0.10-english.tar.xz
mv phpMyAdmin-4.0.10-english phpMyAdmin
(3)提供配置文件
代码语言:javascript复制cd phpMyAdmin
cp config.sample.inc.php config.inc.php
vim config.inc.php 设置密码
<img src="https://www.magedu.com/wp-content/uploads/2020/06/b06bb3322cbb47dca69bf83f91201807.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
重新加载服务:
代码语言:javascript复制systemctl reload httpd
<img src="https://www.magedu.com/wp-content/uploads/2020/06/b29eee1a34c544b8bdc8939578444184.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
输入数据库的账号密码即可登录:
<img src="https://www.magedu.com/wp-content/uploads/2020/06/6a29e433961f4f2d9ec8ed586a9acc51.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
注:如果出现这样的问题:The json extension is missing.
<img src="https://www.magedu.com/wp-content/uploads/2020/06/8968553f62514cbfbbf3d79d95d46b4d.jpg" alt="「敲黑板」小白必看:Centos 7 搭建LAMP(rpm方式)" />
解决办法是:
代码语言:javascript复制vim /etc/php.ini
#加入这一行
extention=json.so
按照以上步骤操作可轻松实现Centos 7 搭建LAMP(rpm方式),建议保存!