目录
什么是LAMP?
LAMP 架构是目前成熟的企业网站应用模式之一,能够提供动态 Web 站点服务及其应用开发环境
具体包括:
- Linux 操作系统
- Apache 网站服务器
- MySQL(有时也指MariaDB,数据库软件) 数据库服务器
- PHP(有时也是指Perl或Python)网页编程语言
优点
- 成本低廉
- 可定制
- 易于开发
- 方便易用
- 安全和稳定
在构建 LAMP 平台时,各组件的安装顺序依次为:Linux、Apache、MySQL、PHP
其中 Apache 和 MySQL 的安装没有严格顺序;PHP一般放在最后,负责沟通 web 服务器和数据库系统。
实验准备(在VMware中进行)
实验说明:因为我们这里要做库站分离,所以单独在一台主机上安装MySQL(或MariaDB)
- 主机A:RHEL7.4 安装Apache和PHP
- IP地址:192.168.112.187
- 主机B:RHEL7.4 安装MySQL(或者MariaDB)
- IP地址:192.168.112.186
主机A:
一、关闭防火墙及Selinux
[root@localhost yum.repos.d]# systemctl stop firewalld.service [root@localhost yum.repos.d]# systemctl disable firewalld.service [root@localhost yum.repos.d]# setenforce 0 [root@localhost yum.repos.d]# vim /etc/selinux/config SELINUX=disabled ---改为disabled
二、更换成阿里云yum源
查询已安装的yum
[root@localhost ~]# rpm -qa | grep yum PackageKit-yum-1.1.5-1.el7.x86_64 yum-3.4.3-154.el7.noarch yum-rhn-plugin-2.0.1-9.el7.noarch yum-utils-1.1.31-42.el7.noarch yum-metadata-parser-1.1.4-10.el7.x86_64 yum-langpacks-0.4.2-7.el7.noarch
使用root权限卸载已安装的yum源
[root@localhost ~]# rpm -qa | grep yum | xargs rpm -e --nodeps warning: /etc/yum/pluginconf.d/langpacks.conf saved as /etc/yum/pluginconf.d/langpacks.conf.rpmsave
下载并安装阿里的yum源rpm包
(浏览器打开网址:https://mirrors.aliyun.com/centos/7/os/x86_64/Packages
然后一个一个的去搜索下载到本地
最后到Linux里面去建一个rpm文件,然后把下载好的包复制进去)
wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-3.4.3-161.el7.centos.noarch.rpm wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-utils-1.1.31-50.el7.noarch.rpm wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-urlgrabber-3.10-9.el7.noarch.rpm
查看rpm包
[root@localhost ~]# mkdir rpm [root@localhost ~]# cd rpm/ [root@localhost rpm]# ls python-urlgrabber-3.10-10.el7.noarch.rpm yum-3.4.3-168.el7.centos.noarch.rpm yum-metadata-parser-1.1.4-10.el7.x86_64.rpm yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm yum-utils-1.1.31-54.el7_8.noarch.rpm
安装(必须要强制安装)
[root@localhost rpm]# rpm -ivh *.rpm --force --nodeps warning: python-urlgrabber-3.10-10.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:yum-metadata-parser-1.1.4-10.el7 ################################# [ 20%] 2:python-urlgrabber-3.10-10.el7 ################################# [ 40%] 3:yum-plugin-fastestmirror-1.1.31-5################################# [ 60%] 4:yum-3.4.3-168.el7.centos ################################# [ 80%] 5:yum-utils-1.1.31-54.el7_8 ################################# [100%]
下载阿里镜像到/etc/yum.repos.d/目录下
[root@localhost ~]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo --2020-12-21 14:36:48-- http://mirrors.aliyun.com/repo/Centos-7.repo Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 112.19.3.184, 112.19.3.182, 112.19.3.183, ... Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|112.19.3.184|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2523 (2.5K) [application/octet-stream] Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’ 100%[======================================>] 2,523 --.-K/s in 0s 2020-12-21 14:36:48 (140 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523]
修改Centos-7.repo文件将所有$releasever替换为7
[root@localhost yum.repos.d]# vim CentOS-Base.repo 按Ctrl+: 输入:%s/$releasever/7/g
清除缓存并重新生成
[root@localhost yum.repos.d]# yum clean all [root@localhost yum.repos.d]# yum makecache
更新yum包
[root@localhost yum.repos.d]# yum update
三、安装Apache
安装
[root@localhost ~]# yum install httpd
重启
[root@localhost yum.repos.d]# systemctl restart httpd [root@localhost yum.repos.d]# systemctl enable httpd
打开浏览器输入IP地址访问
编辑 /etc/httpd/conf/httpd.conf 配置文件,修改以下四个地方
1、搜索 ServerName 修改为对应的 IP 地址
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf #ServerName www.example.com:80 ---这一行是被注释掉的,而且要改为本机IP ServerName 192.168.112.146:80 ---改为这个
2、搜索 DirectoryIndex 添加 index.php
<IfModule dir_module> DirectoryIndex index.php index.html </IfModule>
3、整合APACHE和PHP使APACHE支持PHP
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType application/x-httpd-php .php ---添加这一行 AddType application/x-httpd-php-source .phps ---还有这一行
4、在末尾添加以下内容
<VirtualHost *:80> DocumentRoot "/var/www/html/" ServerName 192.168.112.146 ---注意IP地址 ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.112.187:9000/var/www/html/$1 <Directory "/var/www/html/"> Options none AllowOverride none Require all granted </Directory> </VirtualHost>
确认无误保存退出
查看配置文件是否有语法错误
[root@liza ~]# httpd -t Syntax OK
启动 Httpd
[root@liza ~]# systemctl restart httpd
四、安装PHP
安装php7的YUM源
[root@liza ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm [root@liza ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装PHP7.2
[root@liza ~]# yum install php72w php72w-cli php72w-common php72w-gd php72w-ldap php72mbstring php72w-mcrypt php72w-mysql php72w-pdow -y
安装php-fpm并启动
[root@liza ~]# yum install php72w-fpm php72w-opcache -y [root@liza ~]# systemctl restart php-fpm.service [root@liza ~]# systemctl enable php-fpm.service Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
编辑 php 配置文件
[root@liza ~]# vim /etc/php-fpm.d/www.conf 设置对应的IP监听地址 listen = 192.168.112.187:9000
[root@liza ~]# vim /etc/php-fpm.d/www.conf 设置 httpd 服务器 IP地址 listen.allowed_clients = 192.168.112.187
重启 PHP
[root@liza ~]# systemctl restart php-fpm.service
查看状态
[root@liza ~]# systemctl status php-fpm.service
编辑Apache测试网页
[root@liza ~]# cd /var/www/html/ [root@liza html]# vim index.html <html>Hello World </html>
打开浏览器测试
编辑PHP测试网站
[root@liza ~]# cd /var/www/html/ [root@liza html]# vim index.php <?php phpinfo(); ?>
打开浏览器测试
主机B:
一、关闭防火墙及Selinux
[root@liza ~]# systemctl stop firewalld.service [root@liza ~]# systemctl disable firewalld.service [root@liza ~]# setenforce 0 [root@liza ~]# vim /etc/selinux/config SELINUX=disabled ---改为disabled
二、更换成阿里云yum源
这里和主机A的步骤一样
三、安装MySQL
下载安装MySQL
[root@liza ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm [root@liza ~]# rpm -Uvh mysql80-community-release-el7-2.noarch.rpm [root@liza ~]# yum install mysql-community-server -y
启动MySQL
[root@liza ~]# systemctl restart mysqld [root@liza ~]# systemctl enable mysqld [root@liza ~]# systemctl status mysqld
通过grep过滤出他的默认密码
[root@liza ~]# grep 'password' /var/log/mysqld.log 2021-01-22T06:56:39.471382Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: hetYQdNhZ9/1
使用默认密码登录数据库
[root@liza ~]# mysql -uroot -p'hetYQdNhZ9/1' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.23 Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
更改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'XXGC.lab123'; Query OK, 0 rows affected (0.01 sec) mysql> exit Bye
重启MySQL数据库并设置开机自启
[root@liza ~]# systemctl restart mysqld [root@liza ~]# systemctl enable mysqld
创建MySQL数据库管理员
[root@liza ~]# mysql -uroot -p'XXGC.lab123' mysql> create user 'dbadmin'@'%' identified with mysql_native_password by 'XXGC.lab123'; Query OK, 0 rows affected (0.01 sec) mysql> grant all on *.* to 'dbadmin'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> grant GRANT OPTION on *.* to 'dbadmin'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye
验证是否可以远程登录
[root@liza ~]# mysql -h192.168.112.186 -P3306 -uroot -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'liza' (using password: YES) [root@liza ~]# mysql -h192.168.112.186 -P3306 -udbadmin -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 8.0.23 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
三、实现php连接mysql
回到主机A上面
测试是否可以连接MySQL数据库文件
[root@liza ~]# cd /var/www/html/ [root@liza html]# vim mysql.php <?PHP $conn=mysqli_connect("192.168.112.186","dbadmin","XXGC.lab123"); if($conn){ echo"ok"; }else{ echo"error"; } ?>
并在浏览器上访问
部署wordpress博客
一、下载安装包
可进入网站自行下载 或上传Discuz压缩包到虚拟机
地址为:https://cn.wordpress.org/download/
下载好以后复制到主机A的rpm包文件里面
解压
[root@liza rpm]# tar -zxvf wordpress-5.6-zh_CN.tar.gz [root@liza rpm]# mv wordpress /var/www/html/ ---把包移动到Apache网页下
解决所有文件权限,需要可写权限
[root@liza ~]# cd /var/www/html/ [root@liza html]# chmod -R 777 wordpress/
建立Wordppress博客所用数据库
[root@liza ~]# mysql -uroot -p'XXGC.lab123' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 8.0.23 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database wordpress; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | wordpress | +--------------------+ 5 rows in set (0.00 sec)
二、安装博客
在客户端使用浏览器打开
网站地址:http://192.168.112.187/wordpress
到这里就结束了