在CentOS上部署ThinkPHP項目,可以按照以下步驟進行:
首先,確保你的CentOS系統已經安裝了以下軟件:
sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo yum install php-fpm php-mysqlnd php-gd php-mbstring php-xml php-pear php-bcmath
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
sudo yum install mysql-server
sudo systemctl start mysqld
sudo systemctl enable mysqld
編輯Nginx配置文件,通常位于 /etc/nginx/nginx.conf
或 /etc/nginx/conf.d/default.conf
。
server {
listen 80;
server_name your_domain.com; # 替換為你的域名或IP地址
root /path/to/your/thinkphp/project; # 替換為你的ThinkPHP項目路徑
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
保存并退出編輯器,然后重啟Nginx:
sudo systemctl restart nginx
確保PHP-FPM配置正確,通常位于 /etc/php-fpm.d/www.conf
。
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx
重啟PHP-FPM:
sudo systemctl restart php-fpm
將你的ThinkPHP項目文件上傳到 /path/to/your/thinkphp/project
目錄下。
如果你的項目使用數據庫,確保在MySQL中創建相應的數據庫和用戶,并授權訪問。
CREATE DATABASE your_database_name;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
在ThinkPHP項目的 .env
文件中配置數據庫連接信息:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
進入項目目錄并運行以下命令來安裝依賴:
cd /path/to/your/thinkphp/project
composer install
然后啟動項目:
cd /path/to/your/thinkphp/project
php run start
打開瀏覽器,訪問 http://your_domain.com
,你應該能夠看到你的ThinkPHP項目運行起來了。
以上步驟涵蓋了在CentOS上部署ThinkPHP項目的基本流程。根據你的具體需求,可能還需要進行一些額外的配置和優化。