在Ubuntu上配置Laravel环境需要以下几个步骤:
php -v
如果没有安装PHP,请使用以下命令安装:
sudo apt-get update
sudo apt-get install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo apt-get update
sudo apt-get install nginx
启动Nginx服务:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo nano /etc/nginx/sites-available/yourdomain.com
将以下内容粘贴到文件中,替换yourdomain.com为你的域名,并将/var/www/yourdomain.com/public替换为你的Laravel项目的实际路径:
server {
listen 80;
server_name yourdomain.com;
root /var/www/yourdomain.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
保存并退出编辑器。创建一个符号链接,将新配置文件链接到sites-enabled目录:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled
测试Nginx配置是否正确:
sudo nginx -t
如果一切正常,重启Nginx服务:
sudo systemctl restart nginx
cd /var/www/yourdomain.com
composer create-project --prefer-dist laravel/laravel yourprojectname
将yourprojectname替换为你的项目名称。
sudo chown -R www-data:www-data /var/www/yourdomain.com
sudo chmod -R 755 /var/www/yourdomain.com/storage
sudo chmod -R 755 /var/www/yourdomain.com/bootstrap/cache
.env文件,配置数据库连接信息:nano /var/www/yourdomain.com/.env
根据你的数据库设置,填写以下内容:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=yourdbname
DB_USERNAME=yourdbuser
DB_PASSWORD=yourdbpassword
保存并退出编辑器。
cd /var/www/yourdomain.com
php artisan migrate
现在,你的Laravel环境已经在Ubuntu上配置完成了。访问你在Nginx配置中设置的域名,你应该能看到Laravel的欢迎页面。