第418集集群架构-LAMP动态网站架构
|字数总计:3.3k|阅读时长:15分钟|阅读量:
集群架构 - LAMP动态网站架构
1. LAMP架构-基础架构
后续的所有开源系统部署,都基于该环境之上完成。
1.1 基础环境
1 2 3 4 5 6 7
| [root@apache ~] [root@apache ~]
[root@apache ~] [root@apache ~]
|
1.2 安装LAMP架构
检查并移除旧版PHP
安装PHP 7.2
1 2 3 4 5 6 7 8
| [root@http-server ~] [root@http-server ~]
[root@http-server ~] php72-php-pecl-mysql php72-php-pecl-mysql-xdevapi php72-php-opcache \ php72-php-pecl-memcache php72-php-pecl-memcached php72-php-pecl-redis php72-php-pecl-mcrypt
|
PHP模块说明:
php72-php: PHP核心
php72-php-gd: 图像处理库
php72-php-mysqlnd: MySQL原生驱动
php72-php-opcache: OPcache加速
php72-php-pecl-memcache: Memcache扩展
php72-php-pecl-redis: Redis扩展
php72-php-pecl-mcrypt: 加密扩展
PHP基础优化
1 2 3 4 5 6 7 8
| [root@http-server ~]
date.timezone = Asia/Shanghai upload_max_filesize = 1024M post_max_size = 1024M memory_limit = 1024M
|
优化说明:
date.timezone: 设置时区
upload_max_filesize: 最大上传文件大小
post_max_size: POST数据最大大小
memory_limit: 脚本内存限制
启动Apache服务
1 2 3 4 5 6
| [root@http-server ~] [root@http-server ~]
[root@http-server ~]
|
1.3 安装MariaDB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| [root@http-server ~]
[root@http-server ~] [root@http-server ~]
[root@http-server ~]
Set root password? [Y/n] Y New password: 输入密码123 Re-enter new password: 确认密码123
[root@http-server ~] MariaDB [(none)]> quit
|
1.4 验证LAMP环境
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| cat > /var/www/html/info.php <<EOF <?php phpinfo(); ?> EOF
curl http://localhost/info.php
cat > /var/www/html/db.php <<EOF <?php \$link = mysqli_connect('localhost', 'root', '123'); if (\$link) { echo "Database connection successful!"; } else { echo "Database connection failed!"; } mysqli_close(\$link); ?> EOF
|
2. LAMP架构-部署论坛系统Discuz
2.1 配置Discuz虚拟主机
配置discuz虚拟主机,域名为discuz.bgx.com。
1 2 3 4 5 6 7 8 9 10 11 12
| <VirtualHost *:80> ServerName discuz.bgx.com DocumentRoot "/code/discuz" DirectoryIndex index.php index.html ErrorLog logs/error_discuz_log CustomLog logs/access_discuz_log combined </VirtualHost>
<Directory /code/discuz> AllowOverride All Require all granted </Directory>
|
2.2 部署Discuz源代码
获取Discuz代码
1 2 3 4 5
| [root@http-server ~]
[root@http-server ~]
|
拷贝代码至站点目录
1 2 3 4 5 6 7 8 9 10 11
| [root@http-server ~]
[root@http-server ~]
[root@http-server ~]
[root@http-server ~]
|
目录权限说明:
config: 配置文件目录
data: 数据目录
uc_client: UCenter客户端
uc_server: UCenter服务端
配置Windows hosts解析
在Windows系统中配置hosts解析,以便通过域名访问站点。
文件路径: C:\Windows\system32\drivers\etc\hosts
1
| 192.168.69.112 discuz.bgx.com
|
2.3 通过浏览器访问网站
- 打开浏览器,访问
http://discuz.bgx.com
- 按照安装向导完成Discuz安装
- 填写数据库信息:
- 数据库服务器:
localhost
- 数据库用户名:
root
- 数据库密码:
123
- 数据库名:
discuz(需要先创建)
创建Discuz数据库
1 2 3 4 5 6 7
| [root@http-server ~]
MariaDB [(none)]> create database discuz character set utf8mb4 collate utf8mb4_unicode_ci; Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> exit
|
3. LAMP架构-部署博客系统Wordpress
3.1 配置Wordpress虚拟主机
配置wordpress虚拟主机,域名为blog.bgx.com。
1 2 3 4 5 6 7 8 9 10 11 12
| <VirtualHost *:80> ServerName blog.bgx.com DocumentRoot "/code/wordpress" DirectoryIndex index.php index.html ErrorLog logs/error_wordpress_log CustomLog logs/access_wordpress_log combined </VirtualHost>
<Directory /code/wordpress> AllowOverride All Require all granted </Directory>
|
3.2 部署Wordpress源代码
获取Wordpress代码
1 2 3 4 5 6
| [root@http-server ~] [root@http-server ~]
[root@http-server /soft/src]
|
解压并部署
1 2 3 4 5 6 7 8
| [root@http-server /soft/src]
[root@http-server /soft/src]
[root@http-server ~]
|
创建Wordpress数据库
由于Wordpress无法自动创建数据库,所以需要手动建立数据库。
1 2 3 4 5 6 7
| [root@http-server ~]
MariaDB [(none)]> create database wordpress character set utf8mb4 collate utf8mb4_unicode_ci; Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> exit
|
配置Windows hosts解析
文件路径: C:\Windows\system32\drivers\etc\hosts
1
| 192.168.69.112 discuz.bgx.com blog.bgx.com
|
3.3 通过浏览器访问网站
- 打开浏览器,访问
http://blog.bgx.com
- 按照安装向导完成Wordpress安装
- 填写数据库信息:
- 数据库名:
wordpress
- 用户名:
root
- 密码:
123
- 数据库主机:
localhost
- 表前缀:
wp_(默认)
4. LAMP架构-部署知乎系统Wecenter
4.1 配置Wecenter虚拟主机
配置wecenter虚拟主机,域名为zh.bgx.com。
1 2 3 4 5 6 7 8 9 10 11 12
| <VirtualHost *:80> ServerName zh.bgx.com DocumentRoot "/code/wecenter" DirectoryIndex index.php index.html ErrorLog logs/error_wecenter_log CustomLog logs/access_wecenter combined </VirtualHost>
<Directory /code/wecenter> AllowOverride All Require all granted </Directory>
|
4.2 部署Wecenter源代码
获取Wecenter代码
1 2 3 4 5
| [root@http-server ~]
[root@http-server /soft/src]
|
解压并部署
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [root@http-server ~]
[root@http-server /soft/src]
[root@http-server /soft/src]
[root@http-server ~]
[root@http-server ~]
|
创建Wecenter数据库
1 2 3 4 5 6 7 8
| [root@http-server ~]
MariaDB [(none)]> create database wecenter character set utf8mb4 collate utf8mb4_unicode_ci; Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> quit Bye
|
配置Windows hosts解析
文件路径: C:\Windows\system32\drivers\etc\hosts
1
| 192.168.69.112 discuz.bgx.com blog.bgx.com zh.bgx.com
|
4.3 通过浏览器访问网站
- 打开浏览器,访问
http://zh.bgx.com
- 按照安装向导完成Wecenter安装
- 填写数据库信息:
- 数据库主机:
localhost
- 数据库名:
wecenter
- 数据库用户名:
root
- 数据库密码:
123
5. LAMP架构-部署网校系统Edusohu
5.1 配置Edusohu虚拟主机
配置edusohu虚拟主机,域名为edu.bgx.com。
1 2 3 4 5 6 7 8 9 10 11 12
| <VirtualHost *:80> ServerName edu.bgx.com DocumentRoot "/code/edu/web" DirectoryIndex app.php index.php index.html ErrorLog logs/error_edu_log CustomLog logs/access_edu_log combined </VirtualHost>
<Directory /code/edu/web> AllowOverride All Require all granted </Directory>
|
5.2 部署Edusohu源代码
获取Edusohu代码
1 2 3 4 5
| [root@http-server ~]
[root@http-server /soft/src]
|
解压并部署
1 2 3 4 5 6 7 8 9 10 11
| [root@http-server /soft/src]
[root@http-server /soft/src]
[root@http-server ~]
[root@http-server ~]
|
目录权限说明:
配置Windows hosts解析
由于edusohu会自动创建数据库,所以无需手动创建数据库。
文件路径: C:\Windows\system32\drivers\etc\hosts
1
| 192.168.69.112 discuz.bgx.com blog.bgx.com edu.bgx.com
|
5.3 通过浏览器访问网站
- 打开浏览器,访问
http://edu.bgx.com
- 按照安装向导完成Edusohu安装
- 填写数据库信息:
- 数据库主机:
localhost
- 数据库名:
edusoho(系统会自动创建)
- 数据库用户名:
root
- 数据库密码:
123
6. LAMP架构-迁移数据至独立服务器
迁移LAMP的数据库到独立的数据库服务器步骤。
6.1 老服务器操作
步骤1: 从老的数据库里导出数据
1 2 3 4 5 6
| [root@Httpd ~]
[root@Httpd ~] -rw-r--r-- 1 root root 1.3M Nov 15 10:30 /tmp/backup.sql.gz
|
命令参数说明:
-A: 导出所有数据库
-B: 包含CREATE DATABASE语句
--events: 导出事件
gzip: 压缩备份文件
步骤2: 拷贝到独立的数据库服务器
1 2 3
| [root@Httpd ~] backup.sql.gz 100% 1347KB 21.9MB/s 00:00
|
6.2 新服务器操作
步骤1: 导入数据库
1 2 3 4 5
| [root@MySQL ~]
[root@MySQL ~]
|
步骤2: 重新授权
1 2 3 4 5 6 7 8 9 10 11 12
| [root@MySQL ~]
MariaDB [(none)]> grant all on *.* to remote_user@'192.168.69.%' identified by '123456'; Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
|
授权说明:
remote_user: 远程用户名
192.168.69.%: 允许访问的网段
123456: 远程用户密码
6.3 Web服务器上修改程序连接文件
修改对应MySQL连接配置。
修改Wordpress连接
1 2 3 4 5 6 7 8
| define('DB_HOST', '192.168.69.113');
define('DB_USER', 'remote_user');
define('DB_PASSWORD', '123456');
|
修改Discuz连接
1 2 3
| $_config['db']['1']['dbhost'] = '192.168.69.113'; $_config['db']['1']['dbuser'] = 'remote_user'; $_config['db']['1']['dbpw'] = '123456';
|
1 2 3
| define('UC_DBHOST', '192.168.69.113'); define('UC_DBUSER', 'remote_user'); define('UC_DBPW', '123456');
|
修改Wecenter连接
1 2 3 4 5
| $config['master'] = array( 'hostname' => '192.168.69.113', 'username' => 'remote_user', 'password' => '123456', );
|
修改Edusohu连接
1 2 3
| database_host: 192.168.69.113 database_user: remote_user database_password: 123456
|
6.4 验证数据库连接
1 2 3 4 5
| [root@http-server ~]
MariaDB [(none)]> show databases;
|
7. LAMP架构优化建议
7.1 Apache优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so
<IfModule prefork.c> StartServers 8 MinSpareServers 5 MaxSpareServers 20 ServerLimit 256 MaxClients 256 MaxRequestsPerChild 4000 </IfModule>
|
7.2 PHP优化
1 2 3 4 5 6
| opcache.enable=1 opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4000 opcache.revalidate_freq=60
|
7.3 MySQL优化
1 2 3 4 5 6 7
| [mysqld] innodb_buffer_pool_size = 1G max_connections = 500 query_cache_size = 64M tmp_table_size = 64M max_heap_table_size = 64M
|
7.4 安全加固
1 2 3 4 5 6 7 8 9 10
| mysqladmin -u root -p password '新密码'
mysql -u root -p DROP DATABASE test;
vim /etc/opt/remi/php72/php.ini disable_functions = exec,passthru,shell_exec,system,proc_open,popen
|
8. 常见问题排查
8.1 权限问题
1 2 3 4 5 6 7
| ls -ld /code/*
chown -R apache:apache /code/* find /code -type d -exec chmod 755 {} \; find /code -type f -exec chmod 644 {} \;
|
8.2 数据库连接问题
1 2 3 4 5 6 7 8
| mysql -h localhost -u root -p123
systemctl status mariadb
tail -f /var/log/mariadb/mariadb.log
|
8.3 PHP错误排查
1 2 3 4 5 6 7
| tail -f /var/log/httpd/error_log
vim /etc/opt/remi/php72/php.ini display_errors = On error_reporting = E_ALL
|
8.4 虚拟主机不生效
1 2 3 4 5 6 7 8
| apachectl -S
apachectl configtest
nslookup discuz.bgx.com
|
9. 备份与恢复
9.1 数据库备份
1 2 3 4 5
| mysqldump -uroot -p123 -A -B --events | gzip > /backup/db_$(date +%Y%m%d).sql.gz
mysqldump -uroot -p123 wordpress | gzip > /backup/wordpress_$(date +%Y%m%d).sql.gz
|
9.2 网站文件备份
1 2 3 4 5
| tar -czf /backup/wordpress_$(date +%Y%m%d).tar.gz /code/wordpress
tar -czf /backup/websites_$(date +%Y%m%d).tar.gz /code
|
9.3 自动备份脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #!/bin/bash
cat > /root/backup.sh <<'EOF'
BACKUP_DIR="/backup" DATE=$(date +%Y%m%d)
mysqldump -uroot -p123 -A -B --events | gzip > $BACKUP_DIR/db_$DATE.sql.gz
tar -czf $BACKUP_DIR/websites_$DATE.tar.gz /code
find $BACKUP_DIR -name "*.gz" -mtime +7 -delete find $BACKUP_DIR -name "*.tar.gz" -mtime +7 -delete EOF
chmod +x /root/backup.sh
echo "0 2 * * * /root/backup.sh" | crontab -
|
实战优化