本文档描述了 CRMEB 项目的部署流程,包括环境配置、部署步骤、服务管理等,旨在规范项目部署,确保项目能够稳定运行。
┌─────────────────────────────────────────────────────┐
│ 服务器 │
├──────────────┬──────────────┬──────────────┬─────────┤
│ Nginx/Apache│ PHP-FPM │ MySQL │ Redis │
└──────────────┴──────────────┴──────────────┴─────────┘
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Web 服务器 │────>│ 应用服务器 │────>│ 数据库服务器 │
├──────────────┤ ├──────────────┤ ├──────────────┤
│ Nginx │ │ PHP-FPM │ │ MySQL │
└──────────────┘ └──────────────┘ └──────────────┘
│ │ │
└──────────────────┼──────────────────┘
▼
┌──────────────┐
│ 缓存服务器 │
├──────────────┤
│ Redis │
└──────────────┘
# 关闭 SELinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
# 关闭防火墙(生产环境建议配置规则)
systemctl stop firewalld
systemctl disable firewalld
# 调整文件描述符
cat >> /etc/security/limits.conf << EOF
* soft nofile 65536
* hard nofile 65536
EOF
# 调整内核参数
cat >> /etc/sysctl.conf << EOF
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_max_tw_buckets = 5000
EOF
sysctl -p
# CentOS 安装 PHP 7.4
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install -y php74w php74w-fpm php74w-cli php74w-mysql php74w-redis php74w-gd php74w-mbstring php74w-xml php74w-zip php74w-opcache
# Ubuntu 安装 PHP 7.4
apt update
apt install -y php7.4 php7.4-fpm php7.4-cli php7.4-mysql php7.4-redis php7.4-gd php7.4-mbstring php7.4-xml php7.4-zip php7.4-opcache
// php.ini 配置
memory_limit = 512M
upload_max_filesize = 20M
post_max_size = 20M
max_execution_time = 300
date.timezone = Asia/Shanghai
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 4000
opcache.revalidate_freq = 60
// www.conf 配置
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.process_idle_timeout = 10s
pm.max_requests = 1000
# crmeb.conf
server {
listen 80;
server_name example.com;
root /data/www/crmeb/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
access_log /data/logs/nginx/crmeb.access.log;
error_log /data/logs/nginx/crmeb.error.log;
}
# CentOS 安装 MySQL 5.7
yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum install -y mysql-community-server
systemctl start mysqld
systemctl enable mysqld
# 获取初始密码
grep 'temporary password' /var/log/mysqld.log
# 安全配置
mysql_secure_installation
# Ubuntu 安装 MySQL 5.7
apt update
apt install -y mysql-server
mysql_secure_installation
# my.cnf 配置
[mysqld]
bind-address = 127.0.0.1
port = 3306
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
user = mysql
# 性能优化
max_connections = 1000
wait_timeout = 60
interactive_timeout = 28800
key_buffer_size = 64M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 16M
# InnoDB 优化
innodb_buffer_pool_size = 1G
innodb_file_per_table = 1
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_method = O_DIRECT
# CentOS 安装 Redis
yum install -y epel-release
yum install -y redis
systemctl start redis
systemctl enable redis
# Ubuntu 安装 Redis
apt update
apt install -y redis-server
systemctl start redis
systemctl enable redis
# redis.conf 配置
bind 127.0.0.1
port 6379
databases 16
dir /var/lib/redis
requirepass your_password
maxmemory 512mb
maxmemory-policy allkeys-lru
save 900 1
save 300 10
save 60 10000
# 克隆代码
git clone https://github.com/crmeb/CRMEB.git /data/www/crmeb
cd /data/www/crmeb
# 切换版本
git checkout tags/v5.6.4
# 安装依赖
composer install --no-dev
# 配置环境变量
cp .env.example .env
# 编辑 .env 文件,配置数据库、Redis 等信息
# 生成密钥
php think key:generate
# 数据库迁移
php think migrate:run
# 生成数据表
php think crmeb:install
# 清除缓存
php think clear
/data/www/ 目录unzip CRMEB_v5.6.4.zip -d /data/www/crmebcomposer install --no-dev# Nginx 启动
systemctl start nginx
systemctl enable nginx
# Apache 启动
systemctl start httpd
systemctl enable httpd
systemctl start php-fpm
systemctl enable php-fpm
# 启动队列(推荐使用 Supervisor 管理)
supervisorctl start crmeb-queue
# Supervisor 配置
[program:crmeb-queue]
command=php /data/www/crmeb/think queue:listen --queue=default --timeout=60
process_name=%(program_name)s_%(process_num)02d
autostart=true
autorestart=true
user=www
numprocs=2
directory=/data/www/crmeb
stdout_logfile=/data/logs/supervisor/crmeb-queue-stdout.log
stderr_logfile=/data/logs/supervisor/crmeb-queue-stderr.log
# 启动长连接服务
php think workerman start --d
# 停止长连接服务
php think workerman stop
# 添加定时任务
crontab -e
# 定时任务配置
* * * * * php /data/www/crmeb/think timer run
0 0 * * * php /data/www/crmeb/think crmeb:backup
http://example.comhttp://example.com/adminhttp://example.com/api/ping# 检查 Nginx 日志
tail -f /data/logs/nginx/crmeb.error.log
# 检查 PHP 错误日志
tail -f /var/log/php-fpm/error.log
# 检查应用日志
tail -f /data/www/crmeb/runtime/log/*.log
# 数据库备份
mysqldump -u username -p database_name > /data/backup/data/$(date +%Y%m%d)_backup.sql
# 代码备份
tar -czf /data/backup/code/$(date +%Y%m%d)_crmeb.tar.gz /data/www/crmeb
# 配置文件备份
tar -czf /data/backup/config/$(date +%Y%m%d)_config.tar.gz /data/www/crmeb/config
# 数据库恢复
mysql -u username -p database_name < /data/backup/data/20240101_backup.sql
# 代码恢复
tar -xzf /data/backup/code/20240101_crmeb.tar.gz -C /data/www/
# 配置文件恢复
tar -xzf /data/backup/config/20240101_config.tar.gz -C /data/www/crmeb/