在Linux系統下管理Laravel項目涉及多個方面,包括安裝必要的軟件、配置Web服務器、版本控制、任務調度、日志管理等。以下是詳細的管理步驟:
sudo apt-get update
sudo apt-get install php php-cli php-fpm php-json php-common php-mysql php-zip php-mbstring php-xml php-bcmath php-curl php-pear php-bcmath
sudo apt-get install git
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo apt-get install nginx
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/your-project
sudo ln -s /etc/nginx/sites-available/your-project /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
sudo apt-get install apache2 libapache2-mod-rewrites
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your-project
sudo a2ensite your-project
sudo a2enmod rewrites
sudo systemctl restart apache2
確保Web服務器配置正確,以便能夠處理Laravel的路由和靜態文件。
server {
listen 80;
server_name yourdomain.com;
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 ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /path/to/your/laravel/public
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</VirtualHost>
使用Git進行版本控制:
cd /path/to/your/laravel/project
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/your-username/your-repository.git
git push -u origin master
使用Laravel的Artisan命令行工具定義周期性任務:
app/Console/Kernel.php
文件:protected function schedule(Schedule $schedule)
{
$schedule->command('your:command')->everyMinute();
}
* * * * * cd /path/to/your/laravel/project && php artisan schedule:run >> /dev/null 2>&1
.env
文件:LOG_CHANNEL=single
LOG_LEVEL=debug
LOG_FILE=laravel.log
tail -f storage/logs/laravel.log
php artisan log:clear
php artisan log:clear --since="2021-01-01"
/etc/logrotate.d/laravel
文件:/path/to/your/laravel/storage/logs/*.log {
daily
missingok
rotate 14
compress
notifempty
create 640 root adm
}
通過以上步驟,你可以在Linux系統下有效地管理Laravel項目,確保項目的順利進行和高質量交付。