在Linux上配置ThinkPHP环境是一个相对简单的过程,以下是详细的步骤:
Ubuntu/Debian:
sudo apt update sudo apt install nginx
CentOS:
sudo yum install epel-release sudo yum install nginx
Ubuntu/Debian:
sudo apt update sudo apt install apache2
CentOS:
sudo yum install httpd
Ubuntu/Debian:
sudo apt install php php-cli php-fpm php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
CentOS:
sudo yum install php php-mysqlnd php-fpm php-gd php-mbstring php-curl php-xml php-pear
编辑 /etc/php/7.x/fpm/pool.d/www.conf
文件,确保以下行没有被注释:
listen = /run/php/php7.x-fpm.sock
重启PHP-FPM服务:
sudo systemctl restart php7.x-fpm
编辑Nginx的默认站点配置文件 /etc/nginx/sites-available/default
,添加以下内容:
server { listen 80; server_name your_domain_or_ip; root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.x-fpm.sock; } location ~ /\.ht { deny all; } }
重启Nginx服务:
sudo systemctl restart nginx
编辑Apache的默认站点配置文件 /etc/apache2/sites-available/000-default.conf
,添加以下内容:
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html DirectoryIndex index.php index.html index.htm <Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
启用站点并重启Apache:
sudo a2ensite 000-default.conf sudo systemctl restart apache2
Ubuntu/Debian:
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
CentOS:
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
使用Composer安装ThinkPHP:
composer create-project topthink/think tp6
或者手动下载并解压ThinkPHP框架,然后配置重写规则(可使用 .htaccess
或 nginx.conf
)。
.env
文件中配置数据库连接信息:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_database_user DB_PASSWORD=your_database_password
进入项目目录并启动内置服务器进行测试(仅用于开发环境):
cd /path/to/your/project php think run
或者将项目部署到Nginx或Apache上,确保你的虚拟主机正确指向 public
目录。
sudo apt install certbot python3-certbot-apache sudo certbot --apache -d your_domain_or_ip