下面按从零开始的思路,给你一份在 Ubuntu 下配置 Laravel 的实用指南(以 Ubuntu 22.04 / 24.04 + Laravel 10/11 为例,其他版本类似)。
sudo apt update
sudo apt upgrade -y
Laravel 11 需要 PHP 8.2+
sudo apt install -y php php-cli php-fpm php-mysql php-mbstring php-xml php-curl php-zip php-gd php-bcmath php-sqlite3
验证:
php -v
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer -V
sudo apt install -y mysql-server
sudo mysql_secure_installation
创建数据库:
CREATE DATABASE laravel_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel'@'localhost';
FLUSH PRIVILEGES;
composer create-project laravel/laravel example-app
cd example-app
composer global require laravel/installer
export PATH="$HOME/.composer/vendor/bin:$PATH"
laravel new example-app
.envcp .env.example .env
php artisan key:generate
编辑数据库配置:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel
DB_PASSWORD=password
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
php artisan serve
访问:
http://localhost:8000
sudo apt install -y nginx
sudo nano /etc/nginx/sites-available/laravel
示例配置:
server {
listen 80;
server_name example.com;
root /var/www/example-app/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
启用站点:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
通常是权限或 .env 问题
php artisan config:clear
php artisan cache:clear
sudo apt install php-bcmath php-intl
php artisan queue:work
Crontab:
* * * * * cd /path && php artisan schedule:run
/var/www/example-app
├── app
├── bootstrap
├── config
├── database
├── public
├── storage
└── vendor
如果你告诉我:
我可以给你更精确的一份配置。