在CentOS環境下配置Laravel,你需要遵循以下步驟:
首先,確保你已經安裝了PHP和Composer。然后,使用Composer全局安裝Laravel安裝器:
composer global require laravel/installer
將Composer的全局二進制文件目錄添加到系統的PATH變量中。編輯~/.bashrc
或~/.bash_profile
文件,添加以下行:
export PATH="$HOME/.composer/vendor/bin:$PATH"
保存文件并運行source ~/.bashrc
或source ~/.bash_profile
使更改生效。
使用Laravel安裝器創建一個新的Laravel項目:
laravel new project_name
將project_name
替換為你的項目名稱。這將在當前目錄下創建一個新的Laravel項目。
在CentOS上,我們通常使用Apache或Nginx作為Web服務器。下面是如何配置這兩個服務器以運行Laravel項目。
Apache配置:
啟用mod_rewrite模塊:
sudo yum install mod_rewrite
sudo systemctl enable httpd
sudo systemctl start httpd
在/etc/httpd/conf/httpd.conf
文件中,找到<Directory>
部分并啟用AllowOverride All
:
<Directory /var/www/html>
AllowOverride All
</Directory>
將Laravel項目移動到Apache的默認文檔根目錄(例如/var/www/html
),或者更新DocumentRoot指向你的項目目錄。
重啟Apache服務:
sudo systemctl restart httpd
Nginx配置:
安裝Nginx:
sudo yum install epel-release
sudo yum install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
在/etc/nginx/conf.d/default.conf
文件中,添加以下配置:
server {
listen 80;
server_name example.com; # 替換為你的域名或公網IP
root /path/to/your/laravel/project; # 替換為你的Laravel項目路徑
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:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
將example.com
替換為你的域名或公網IP,將/path/to/your/laravel/project
替換為你的Laravel項目路徑。
重啟Nginx服務:
sudo systemctl restart nginx
確保Laravel項目的storage
和bootstrap/cache
目錄具有可寫權限:
sudo chown -R nginx:nginx /path/to/your/laravel/project/storage
sudo chown -R nginx:nginx /path/to/your/laravel/project/bootstrap/cache
將nginx:nginx
替換為你的Web服務器用戶和組,將/path/to/your/laravel/project
替換為你的Laravel項目路徑。
現在,你應該可以在瀏覽器中訪問你的Laravel項目了。如果遇到任何問題,請檢查Web服務器和Laravel的日志以獲取更多信息。