在CentOS上部署Laravel多實例,您需要按照以下步驟操作:
首先,確保您已經安裝了Nginx和PHP-FPM。如果沒有,請使用以下命令安裝:
sudo yum install epel-release
sudo yum install nginx
sudo yum install php-fpm
編輯PHP-FPM配置文件,為每個Laravel實例創建一個單獨的pool配置。例如,您可以創建兩個實例,分別命名為laravel1和laravel2。為此,請復制默認的PHP-FPM pool配置文件,并根據需要進行修改。
sudo cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/laravel1.conf
sudo cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/laravel2.conf
接下來,編輯laravel1.conf和laravel2.conf文件,將listen指令更改為不同的端口,例如:
; laravel1.conf
listen = /run/php-fpm/laravel1.sock
; laravel2.conf
listen = /run/php-fpm/laravel2.sock
然后,重啟PHP-FPM服務以應用更改:
sudo systemctl restart php-fpm
為每個Laravel實例創建一個單獨的Nginx服務器塊配置文件。例如,您可以創建兩個配置文件,分別命名為laravel1.conf和laravel2.conf。為此,請復制默認的Nginx服務器塊配置文件,并根據需要進行修改。
sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/laravel1.conf
sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/laravel2.conf
接下來,編輯laravel1.conf和laravel2.conf文件,根據需要修改server_name、root和fastcgi_pass指令。例如:
# laravel1.conf
server {
listen 80;
server_name laravel1.example.com;
root /path/to/laravel1/public;
index index.php index.html index.htm;
location /
{
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$
{
fastcgi_pass unix:/run/php-fpm/laravel1.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
# laravel2.conf
server {
listen 80;
server_name laravel2.example.com;
root /path/to/laravel2/public;
index index.php index.html index.htm;
location /
{
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$
{
fastcgi_pass unix:/run/php-fpm/laravel2.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
確保將laravel1.example.com和laravel2.example.com替換為您的實際域名,并將/path/to/laravel1/public和/path/to/laravel2/public替換為您的Laravel項目的實際路徑。
應用更改后,重啟Nginx服務:
sudo systemctl restart nginx
如果您的服務器啟用了防火墻,請確保開放HTTP(端口80)和HTTPS(端口443)流量:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
如果您在本地開發環境中使用自定義域名,請更新/etc/hosts文件,將域名映射到服務器IP地址:
127.0.0.1 laravel1.example.com
127.0.0.1 laravel2.example.com
現在,您應該可以通過訪問http://laravel1.example.com和http://laravel2.example.com來訪問您的Laravel多實例。