阅读量:2
在Debian上设置Laravel的多环境(例如本地开发、测试和生产)可以通过以下步骤完成。我们将使用Nginx作为Web服务器,并使用PHP-FPM来处理PHP请求。
1. 安装必要的软件
首先,更新你的包列表并安装Nginx、PHP-FPM和其他必要的软件:
sudo apt update
sudo apt install nginx php-fpm php-mysql php-curl php-xml php-mbstring php-zip unzip
2. 配置PHP-FPM
编辑PHP-FPM配置文件以适应你的需求:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
确保以下行没有被注释掉:
listen = /run/php/php7.4-fpm.sock
listen.owner = www-data
listen.group = www-data
保存并退出编辑器,然后重启PHP-FPM服务:
sudo systemctl restart php7.4-fpm
3. 配置Nginx
为每个环境创建一个Nginx服务器块配置文件。
本地开发环境
sudo nano /etc/nginx/sites-available/local
添加以下内容:
server {
listen 80;
server_name localhost;
root /var/www/local;
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.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
启用该配置文件:
sudo ln -s /etc/nginx/sites-available/local /etc/nginx/sites-enabled/
测试环境
sudo nano /etc/nginx/sites-available/staging
添加以下内容:
server {
listen 80;
server_name staging.example.com;
root /var/www/staging;
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.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
启用该配置文件:
sudo ln -s /etc/nginx/sites-available/staging /etc/nginx/sites-enabled/
生产环境
sudo nano /etc/nginx/sites-available/production
添加以下内容:
server {
listen 80;
server_name production.example.com;
root /var/www/production;
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.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
启用该配置文件:
sudo ln -s /etc/nginx/sites-available/production /etc/nginx/sites-enabled/
4. 配置hosts文件
编辑本地hosts文件以便在本地访问不同的环境:
sudo nano /etc/hosts
添加以下行:
127.0.0.1 localhost
127.0.0.1 staging.example.com
127.0.0.1 production.example.com
5. 重启Nginx
最后,重启Nginx以应用所有更改:
sudo systemctl restart nginx
6. 设置文件权限
确保你的Laravel项目目录具有正确的权限:
sudo chown -R www-data:www-data /var/www/local
sudo chown -R www-data:www-data /var/www/staging
sudo chown -R www-data:www-data /var/www/production
7. 配置Laravel环境变量
在每个环境的根目录下创建或编辑.env文件,设置相应的环境变量。例如:
本地开发环境
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=local_db
DB_USERNAME=root
DB_PASSWORD=
测试环境
APP_ENV=staging
APP_DEBUG=false
APP_URL=http://staging.example.com
DB_CONNECTION=mysql
DB_HOST=staging_host
DB_PORT=3306
DB_DATABASE=staging_db
DB_USERNAME=staging_user
DB_PASSWORD=staging_password
生产环境
APP_ENV=production
APP_DEBUG=false
APP_URL=http://production.example.com
DB_CONNECTION=mysql
DB_HOST=production_host
DB_PORT=3306
DB_DATABASE=production_db
DB_USERNAME=production_user
DB_PASSWORD=production_password
完成这些步骤后,你就可以在Debian上成功设置Laravel的多环境了。
以上就是关于“Laravel在Debian上的多环境如何设置”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm