在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多实例。