在 CentOS 上安装 Laravel 并迁移项目,可以按照以下步骤进行:
安装必要的软件包:
sudo yum update -y
sudo yum install php php-cli php-fpm php-mysqlnd php-mbstring php-xml php-zip -y
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
配置 Nginx:
sudo nano /etc/nginx/conf.d/laravel.conf
server {
listen 80;
server_name your_domain_or_ip;
root /path/to/your/laravel/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm 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-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
sudo systemctl restart nginx
创建 Laravel 项目:
composer create-project --prefer-dist laravel/laravel your_project_name
cd your_project_name
配置数据库:
.env
文件,设置数据库连接信息:DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
sudo mysql -u your_database_username -p your_database_name < your_database_dump.sql
运行数据库迁移:
php artisan key:generate
php artisan migrate
访问项目:
http://your_domain_or_ip
,你应该能够看到 Laravel 的欢迎页面。通过以上步骤,你已经在 CentOS 上成功安装了 Laravel 并迁移了项目。