zh
Laravel VPS托管:PHP应用部署完整指南
Laravel是PHP生态中最优雅、最功能完整的Web框架,以其ORM、队列系统、任务调度和强大的生态工具著称。将Laravel应用迁移到专属VPS意味着摆脱共享主机的资源限制,获得完整的服务器控制权。Anubiz Host冰岛VPS提供Laravel运行所需的全栈环境支持,帮助开发者以最低成本构建高性能PHP应用。
Need this done for your project?
We implement, you ship. Async, documented, done in days.
LEMP环境安装(Linux+Nginx+MySQL+PHP)
Laravel需要PHP 8.2+、MySQL/MariaDB和Nginx(或Apache)。在Ubuntu VPS上安装完整LEMP栈:
apt update && apt upgrade -y
apt install -y nginx mysql-server
# 安装PHP 8.2和必要扩展
apt install -y software-properties-common
add-apt-repository ppa:ondrej/php
apt update
apt install -y php8.2-fpm php8.2-cli php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl php8.2-zip php8.2-bcmath php8.2-tokenizer php8.2-gd php8.2-redis
安装Composer(PHP依赖管理器):
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer
运行MySQL安全初始化:
mysql_secure_installationLaravel项目部署与配置
将Laravel项目部署到VPS并配置.env文件:
cd /var/www
git clone https://github.com/youruser/yourproject.git laravel-app
cd laravel-app
composer install --optimize-autoloader --no-dev
cp .env.example .env
php artisan key:generate
配置.env文件中的关键参数:
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=your_strong_password
执行数据库迁移和优化:
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
设置正确的文件权限:
chown -R www-data:www-data /var/www/laravel-app
chmod -R 755 /var/www/laravel-app/storage
chmod -R 755 /var/www/laravel-app/bootstrap/cacheNginx与PHP-FPM配置
为Laravel应用创建Nginx站点配置:
nano /etc/nginx/sites-available/laravel-app
Laravel官方推荐的Nginx配置:
server {
listen 80;
server_name yourdomain.com;
root /var/www/laravel-app/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ .php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.(?!well-known).* {
deny all;
}
}
ln -s /etc/nginx/sites-available/laravel-app /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
certbot --nginx -d yourdomain.comRedis缓存与队列配置
Laravel的队列系统和缓存层通过Redis可以显著提升应用性能。安装Redis并配置Laravel:
apt install -y redis-server
systemctl enable redis-server
systemctl start redis-server
在.env文件中启用Redis:
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
创建Laravel队列worker的systemd服务:
nano /etc/systemd/system/laravel-worker.service
[Unit]
Description=Laravel Queue Worker
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/laravel-app
ExecStart=/usr/bin/php artisan queue:work redis --sleep=3 --tries=3
Restart=always
[Install]
WantedBy=multi-user.target
systemctl enable laravel-worker
systemctl start laravel-worker
使用Laravel Scheduler配合crontab实现定时任务自动化,是替代复杂cron规则的优雅方案。Related Services
Why Anubiz Host
100% async — no calls, no meetings
Delivered in days, not weeks
Full documentation included
Production-grade from day one
Security-first approach
Post-delivery support included
Ready to get started?
Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.