宝塔、Kangle、Docker、Lnmp搭建ThinkPHP/FastAdmin开发环境
FastAdmin是一款基于ThinkPHP5+Bootstrap的极速后台开发框架。和其他php程序不同支持是需要设置public目录,下面就用多种方法搭建ThinkPHP环境。
XML/HTML代码
- 操作系统:Linux、Windows、Mac OS
- Web 服务器:Apache、Nginx
- PHP 版本:PHP 7.4+ (推荐 PHP 7.4)
- MySQL 版本:>= 5.6 且 <= 8.0 (需支持 innodb 引擎)
一、宝塔
最最简单的一种方法,物理机或者Docker搭建宝塔,安装后按照以上需求安装需要组件。
Linux通用安装脚本(推荐)
XML/HTML代码
- if [ -f /usr/bin/curl ];then curl -sSO https://download.bt.cn/install/install_panel.sh;else wget -O install_panel.sh https://download.bt.cn/install/install_panel.sh;fi;bash install_panel.sh ed8484bec
Docker一键命令(支持ARM平台)
XML/HTML代码
- docker run -d --restart unless-stopped --name baota --net=host -v /opt/bt/website_data:/www/wwwroot -v /opt/bt/mysql_data:/www/server/data -v /opt/bt/vhost:/www/server/panel/vhost btpanel/baota:7.9.4-lnmp
数据映射目录 /opt/bt 可以根据自己实际情况修改
浏览器访问默认地址http://您的ip地址:8888/btpanel
默认用户:btpanel
默认密码:btpaneldocker
容器默认SSH密码:btpaneldocker
注意:此镜像宝塔为7.9.4版本,显示可能不正常,只需要点击右上角升级宝塔到最新版本即可。
按照正常步骤创建网站,正常上传解压FastAdmin源代码到web目录。然后在建好的网站上点击设置--网站目录--运行目录--选择public 伪静态--thinkphp
剩余的修改配置文件,对接数据库,安装FastAdmin。
二、Kangle
Kangle有很多一键脚本,可自行搜索安装,以下以在Ubuntu/Debian上一键安装kangle web server为例演示。
Github:https://github.com/funnycups/kangle (需科学)
XML/HTML代码
- wget -q -O install.sh https://raw.githubusercontent.com/funnycups/kangle/main/install.sh && bash install.sh
输入mysql的root密码继续,选择2裸机安装,3是Docker安装。建议选择2安装。安装完毕后出现服务器信息。
Kangle panel:http://ip:3311
Easypanel:http://ip:3311
用户名:admin 密码:kangle
PHPMyAdmin:http://ip:3313/mysql
Kangle使用方法不再详细说明了,只说FastAdmin,代码上传网站更目录,修改数据库信息,绑定域名时候绑定目录 wwwroot/public 即可。同时在面板伪静态选项修改为thinkphp。如果没有thinkphp选项可以手动输入以下代码
XML/HTML代码
- if (!-e $request_filename) {
- rewrite ^(.*)$ /index.php?s=/$1 last;
- break;
- }
三、Docker
方式1:已有mysql容器,直接run
前提是你有一个正在运行的mysql数据库容器,并且有一个虚拟网络 ,例如名称为:test-net
XML/HTML代码
- docker run -d --name fastmaoshu -p 7898:80 --network test-net maoshufun/fastadmin-nginx
方式2:没有mysql,用docker-compose集成安装
创建 docker-compose.yml 文件
里面的端口信息根据自己需求修改下,以免冲突; 数据库信息也改下
XML/HTML代码
- version: '3'
- services:
- app:
- image: maoshufun/fastadmin-nginx
- container_name: fastadmin-app
- restart: always
- ports:
- - "7899:80"
- environment:
- WEB_DOCUMENT_ROOT: /app/public # 设置ThinkPHP项目的public目录为文档根目录
- WEB_ALIAS_DOMAIN: example.com,www.example.com # 可以设置你的域名
- PHP_DISPLAY_ERRORS: "1" # 开启PHP错误显示(仅在开发环境中)
- PHP_XDEBUG: 1 # 如果不需要Xdebug,可以设为0;若需要,请设置为1并参考官方文档配置
- MYSQL_HOST: mysql # MySQL服务的网络别名
- MYSQL_DATABASE: thinkdb
- MYSQL_USER: test
- MYSQL_PASSWORD: 123456
- REDIS_HOST: redis # Redis服务的网络别名
- depends_on:
- - mysql
- - redis
- networks:
- - app-network
- mysql:
- image: mysql:5.7
- container_name: mysql5.70
- restart: always
- ports:
- - "33066:3306"
- environment:
- MYSQL_ROOT_PASSWORD: root123456
- MYSQL_DATABASE: thinkdb
- MYSQL_USER: test
- MYSQL_PASSWORD: 123456
- volumes:
- - ./mysql/data:/var/lib/mysql
- networks:
- - app-network
- redis:
- image: redis:5.0
- container_name: redis5.00
- restart: always
- ports:
- - "63796:6379"
- command: ["redis-server", "--appendonly", "yes"]
- volumes:
- - ./redis/data:/data
- networks:
- - app-network
- networks:
- app-network:
- driver: bridge
如果要使用自己的项目代码,那直接加上路径映射再执行即可,其中tp5里放你的项目代码
注意:创建app-network网络,所有容器都在这个网络里面,数据库直接使用mysql主机名连接,不能使用localhost或者127.0.0.1连接。
其他容器1:
直接创建一个fastadmin容器
XML/HTML代码
- docker run -it -d -p 80:80 mymoyi/fastadmin
XML/HTML代码
- docker run -it -d -p 80:80 -v /moyi/app:/app mymoyi/fastadmin
XML/HTML代码
- docker run -it -d -p 80:80 --network my_network mymoyi/fastadmin
本镜像解决了非宝塔环境下nginx伪静态配置异常复杂的情况。
XML/HTML代码
- docker run -itd --name fastadmin -v /opt/html:/data/www/html -p 80:80 watish/php-fpm-fastadmin
原理和步骤都一样,容器和数据库在一个网络下面,然后对接就可以实现快速搭建FastAdmin环境的目的。
四、Lnmp
可以使用 https://lnmp.org 或者其他一键Lnmp代码搭建,也可以使用全手动搭建。
Debian 12 / Ubuntu 24.04 使用源安装 LEMP 教程
本文将介绍使用官方源和第三方源在 Debian 12 和 Ubuntu 24.04 安装最新版 Nginx + PHP + MySQL 的教程,并且可以自行选择 PHP 版本。
PS:本文同时适用于 Debian 11 Bullseye,Ubuntu 20.04 Focal 以及 Ubuntu 22.04 Jammy
以下操作需要在 root 用户下完成,请使用 sudo -i 或 su root 切换到 root 用户进行操作。
1、更新系统并安装部分必要软件
XML/HTML代码
- apt update
- apt upgrade -y
- apt install curl vim wget gnupg dpkg apt-transport-https lsb-release ca-certificates
如果您通过 iso 方式安装 Debian 12 并且设置了 root 密码,则默认不带 sudo 包,使用 apt install sudo 安装即可
2、增加烧饼博客打包的 Nginx 源并安装
2.1 首先增加 GPG Key
XML/HTML代码
- curl -sSL https://n.wtf/public.key | gpg --dearmor > /usr/share/keyrings/n.wtf.gpg
2.2 然后增加 Nginx 源
XML/HTML代码
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/n.wtf.gpg] https://mirror-cdn.xtom.com/sb/nginx/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/n.wtf.list
国内机器可以用清华 TUNA 的国内源:
XML/HTML代码
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/n.wtf.gpg] https://mirrors.tuna.tsinghua.edu.cn/u.sb/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/n.wtf.list
2.3 接着更新并安装 Nginx
XML/HTML代码
- apt update
- apt install nginx-extras -y
安装完毕后,我们可以使用 nginx -V 命令看到 Nginx 已经是最新的 1.27.0 主线版了:
XML/HTML代码
- root@debian ~ # nginx -V
- nginx version: nginx-n.wtf/1.27.2
- built with OpenSSL 3.4.0 22 Oct 2024
- TLS SNI support enabled
3、增加 Ondřej Surý 大神打包的 PHP 源并安装 PHP 8.x
Ondřej Surý 大佬打包的 PHP 源更好用,Ubuntu 的 PPA for PHP 就是这位大佬做的,当然少不了 Debian 的源了,下面一步一步来。
3.1 Debian 和 Ubuntu 安装 LEMP 区别
唯一区别就是 PHP 的安装添加源方法不一样,其他的步骤都一毛一样。
3.2 加入大神做好的源
XML/HTML代码
- #Debian
- wget -O /usr/share/keyrings/php.gpg https://packages.sury.org/php/apt.gpg
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list
- #Ubuntu
- add-apt-repository ppa:ondrej/php
这个大神的 GPG 密钥每两年会更新一个新的,如果 GPG 密钥失效,重新下载 GPG 密钥即可。
国内机器可以用南京大学的国内源:
XML/HTML代码
- wget -O /usr/share/keyrings/php.gpg https://mirror.nju.edu.cn/sury/php/apt.gpg
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/php.gpg] https://mirror.nju.edu.cn/sury/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list
Ubuntu 的 PPA 暂时没有国内镜像,可以使用 USTC 反代的方式:
XML/HTML代码
- curl "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c" | gpg --dearmor > /usr/share/keyrings/php.gpg
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/php.gpg] https://launchpad.proxy.ustclug.org/ondrej/php/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list
如果 GPG 密钥失效,请查看最新的 GPG 密钥。
3.3 更新系统
XML/HTML代码
- apt update
- apt upgrade -y
3.4 安装自己需要的 PHP 版本
这个源目前默认的 PHP 是 8.4.x,如果您需要其他版本,那么请修改对应的 PHP 版本号 (注意配置文件哦)。
这里举例 WordPress 需要的部分 PHP 包:
安装 PHP 8.4.x:
XML/HTML代码
- apt install php8.4-{fpm,cli,mysql,curl,gd,mbstring,xml,zip,imap,opcache,soap,gmp,bcmath} -y
安装 PHP 8.3.x:
XML/HTML代码
- apt install php8.3-{fpm,cli,mysql,curl,gd,mbstring,xml,zip,imap,opcache,soap,gmp,bcmath} -y
安装 PHP 8.2.x:
XML/HTML代码
- apt install php8.2-fpm php8.2-cli php8.2-mysql php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip php8.2-imap php8.2-opcache php8.2-soap php8.2-gmp php8.2-bcmath -y
安装 PHP 8.1.x:
XML/HTML代码
- apt install php8.1-fpm php8.1-cli php8.1-mysql php8.1-curl php8.1-gd php8.1-mbstring php8.1-xml php8.1-zip php8.1-imap php8.1-opcache php8.1-soap php8.1-gmp php8.1-bcmath -y
以下版本 PHP 已经 EOL,PHP 官方不再提供支持,请尽快更新您的程序兼容最新的 PHP,如果您的程序还未兼容,建议鞭策开发者
安装 PHP 8.0.x:
XML/HTML代码
- apt install php8.0-fpm php8.0-cli php8.0-mysql php8.0-curl php8.0-gd php8.0-mbstring php8.0-xml php8.0-zip php8.0-imap php8.0-opcache php8.0-soap php8.0-gmp php8.0-bcmath -y
安装 PHP 7.4.x:
XML/HTML代码
- apt install php7.4-fpm php7.4-cli php7.4-mysql php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-xmlrpc php7.4-zip php7.4-json php7.4-imap php7.4-opcache php7.4-soap php7.4-gmp php7.4-bcmath -y
安装 PHP 7.3.x:
XML/HTML代码
- apt install php7.3-fpm php7.3-mysql php7.3-curl php7.3-gd php7.3-mbstring php7.3-xml php7.3-xmlrpc php7.3-zip php7.3-opcache
安装 PHP 7.2.x (PHP 7.2 开始已经不支持 mcrypt 组件):
XML/HTML代码
- apt install php7.2-fpm php7.2-mysql php7.2-curl php7.2-gd php7.2-mbstring php7.2-xml php7.2-xmlrpc php7.2-zip php7.2-opcache
安装 PHP 7.1.x:
XML/HTML代码
- apt install php7.1-fpm php7.1-mysql php7.1-curl php7.1-gd php7.1-mbstring php7.1-mcrypt php7.1-xml php7.1-xmlrpc php7.1-zip php7.1-opcache
安装 PHP 7.0.x:
XML/HTML代码
- apt php7.0-fpm php7.0-mysql php7.0-curl php7.0-gd php7.0-mbstring php7.0-mcrypt php7.0-xml php7.0-xmlrpc php7.0-zip php7.0-opcache
安装 PHP 5.6.x:
XML/HTML代码
- apt install php5.6-fpm php5.6-mysql php5.6-curl php5.6-gd php5.6-mbstring php5.6-mcrypt php5.6-xml php5.6-xmlrpc php5.6-zip php5.6-opcache
如果希望安装其他组件,可以通过搜索看看有没有对应的包:
XML/HTML代码
- apt-cache search php8.4* | grep php8.4
修改 php.ini 防止跨目录攻击,如果安装的 PHP 8.4.x 请修改 /etc/php/8.4/fpm/php.ini PHP 7.4.x 请对应修改 /etc/php/7.4/fpm/php.ini:
XML/HTML代码
- sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/8.4/fpm/php.ini
修改 php.ini 增加上传大小限制,比如我们设置 10MB:
XML/HTML代码
- sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 10M/' /etc/php/8.4/fpm/php.ini
- sed -i 's/post_max_size = 8M/post_max_size = 10M/' /etc/php/8.4/fpm/php.ini
您也可以同时安装多个 PHP 版本,然后使用以下命令选择系统默认的 PHP 版本:
XML/HTML代码
- update-alternatives --config php
3.5 重启 PHP 和 Nginx
XML/HTML代码
- systemctl restart php8.4-fpm
对应 PHP 7.4.x 命令如下:
XML/HTML代码
- systemctl restart php7.4-fpm
Nginx 参考配置文件如下,新建立个 /etc/nginx/sites-available/example.com.conf:
XML/HTML代码
- cat >> /etc/nginx/sites-available/example.com.conf << EOF
- server {
- listen 80;
- listen [::]:80;
- # 指定网站目录,可根据自己情况更换,建议放在 /var/www 目录下
- root /var/www/example.com;
- index index.php index.html index.htm;
- # 默认第一个域名,替换 example.com 为您的域名
- server_name example.com;
- location / {
- try_files \$uri \$uri/ =404;
- }
- # 开启 PHP8.4-fpm 模式,如需要安装 PHP 7.4.x 请修改为 fastcgi_pass unix:/run/php/php7.4-fpm.sock;
- location ~ \.php$ {
- include snippets/fastcgi-php.conf;
- fastcgi_pass unix:/run/php/php8.4-fpm.sock;
- }
- }
- EOF
XML/HTML代码
- ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf
到这里基本没有问题,可以直接重新加载 Nginx:
XML/HTML代码
- nginx -t
- nginx -s reload
或者暴力点直接重启 Nginx:
XML/HTML代码
- systemctl restart nginx
我们的目录在 /var/www/example.com,我们先创建这个目录:
XML/HTML代码
- mkdir -p /var/www/example.com
然后创建一个 phpinfo.php 并输入 phpinfo() 函数:
XML/HTML代码
- cat >> /var/www/example.com/phpinfo.php << EOF
- <?php phpinfo(); ?>
- EOF
好了,此时在浏览器输入 http://example.com/phpinfo.php,如果看到经典的 phpinfo 页面则说明安装成功,如果不成功,请仔细对比步骤查找哪里出错。
4、安装 MariaDB
自从 Debian 9.x Stretch 开始,Debian 已经默认使用 Mariadb,所以我们不做对于 MySQL 和 MariaDB 的争论,直接跟着开源社区走即可。
4.1 首先,添加并导入 Mariadb 的官方源
下载 GPG Key:
XML/HTML代码
- curl -sSL https://mariadb.org/mariadb_release_signing_key.asc | gpg --dearmor > /usr/share/keyrings/mariadb.gpg
然后添加 MariaDB 的源:
XML/HTML代码
- #Debian
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mariadb.gpg] https://mirror-cdn.xtom.com/mariadb/repo/11.4/debian $(lsb_release -sc) main" > /etc/apt/sources.list.d/mariadb.list
- #Ubuntu
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mariadb.gpg] https://mirror-cdn.xtom.com/mariadb/repo/11.4/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/mariadb.list
国内可以用清华 TUNA 的源:
XML/HTML代码
- #Debian
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mariadb.gpg] https://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/11.4/debian $(lsb_release -sc) main" > /etc/apt/sources.list.d/mariadb.list
- #Ubuntu
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mariadb.gpg] https://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/11.4/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/mariadb.list
您也可以在这儿找到更多的 MariaDB 源。
4.2 接着更新一下系统
XML/HTML代码
- apt update
4.3 然后直接安装最新稳定版 MariaDB
XML/HTML代码
- apt install mariadb-server mariadb-client
安装完毕后强烈推荐使用 mariadb-secure-installation 命令做一次安全设置。
4.4 创建数据库并测试
开启数据库之前,您可以使用 pwgen 这个小工具或者随机密码生成器生成一个强大的随机密码,比如 32 位,然后随意挑选一个使用
XML/HTML代码
- apt install pwgen
- pwgen 32
使用 Mariadb root 用户登陆,因为默认使用 Unix domain socket 模式,所以本机不需要 MySQL root 密码即可登录:
XML/HTML代码
- mariadb -u root
创建数据库 example_database:
XML/HTML代码
- CREATE DATABASE example_database DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
创建用户名 example_user 并赋予权限:
XML/HTML代码
- GRANT ALL ON example_database.* TO 'example_user'@'localhost' IDENTIFIED BY '这里改成您要设置的强大的没人能猜出来的随机的密码';
刷新 MySQL 权限:
XML/HTML代码
- FLUSH PRIVILEGES;
没问题以后就可以退出了:
XML/HTML代码
- EXIT;
新建立一个 /var/www/example.com/mysql-test.php 文件并测试:
XML/HTML代码
- cat >> /var/www/example.com/mysql-test.php << EOF
- <?php
- \$dbname = 'example_database'; //MySQL 数据库名
- \$dbuser = 'example_user'; //MySQL 用户名
- \$dbpass = '您的强大的没人可以猜出来的密码';
- \$dbhost = 'localhost'; //安装在本地就用 localhost
- \$link = mysqli_connect(\$dbhost, \$dbuser, \$dbpass) or die("Unable to Connect to '\$dbhost'");
- mysqli_select_db(\$link, \$dbname) or die("Could not open the db '\$dbname'");
- \$test_query = "SHOW TABLES FROM \$dbname";
- \$result = mysqli_query(\$link, \$test_query);
- \$tblCnt = 0;
- while(\$tbl = mysqli_fetch_array(\$result)) {
- \$tblCnt++;
- #echo \$tbl[0]."<br />\n";
- }
- if (!\$tblCnt) {
- echo "MySQL is working fine. There are no tables.";
- } else {
- echo "MySQL is working fine. There are \$tblCnt tables.";
- }
- ?>
- EOF
创建完毕后访问 http://example.com/mysql-test.php 如果出现 MySQL is working fine. There are no tables. 则说明 MariaDB 工作正常。
5、安装 MySQL 8.0 (可选)
如果您必须使用某些 MySQL 8.0 才有的功能,那么可以按照 MySQL 官网的教程安装 MySQL 8.0。
*注意:*除非您知道您在做什么,否则不要同时安装 MySQL 和 MariaDB。
5.1 添加 apt 源
XML/HTML代码
- wget https://repo.mysql.com/mysql-apt-config_0.8.33-1_all.deb
- dpkg -i mysql-apt-config_0.8.33-1_all.deb
国内的机器可以在添加完成后修改为清华 TUNA 源,您可以修改 /etc/apt/sources.list.d/mysql-community.list 文件,替换成如下内容:
XML/HTML代码
- deb https://mirrors.tuna.tsinghua.edu.cn/mysql/apt/debian $(lsb_release -sc) mysql-5.6 mysql-5.7 mysql-8.0 mysql-tools
5.2 安装 MySQL 8.0
XML/HTML代码
- apt update
- apt install mysql-server -y
默认 MySQL 会安装最新的 8.0 版本,如果您需要更低的版本,比如 5.6 或 5.7,可以使用如下命令:
XML/HTML代码
- dpkg-reconfigure mysql-apt-config
您可能需要设置一个强大的 root 密码,接下来的步骤和 MariaDB 基本相同,把 mariadb 命令换成 mysql 命令即可,本文不再赘述。
针对ThinkPHP/FastAdmin的nginx配置文件参考如下
XML/HTML代码
- server {
- listen 80;
- listen [::]:80;
- server_name _;
- root /var/www/fastadmin/public;
- location / {
- index index.html index.htm index.php;
- if (!-e $request_filename) {
- rewrite ^/(.*)$ /index.php/$1 last;
- }
- }
- location ~ \.php(.*)$ {
- fastcgi_pass unix:/run/php/php7.4-fpm.sock;
- fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
- include snippets/fastcgi-php.conf;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- }
- }
详细配置根据自己实际情况进行修改。
ThinkPHP/FastAdmin数据库对接文件 .env.sample 修改后改名 .env 即可。