Laravel 5.4 项目部署至虚拟主机遇问题求助
Hey there! I’ve helped tons of developers work through Laravel deployment snags on shared hosts, so let’s break down the most common issues that might be tripping you up—even after you’ve checked off the initial setup steps.
1. Double-Check Your index.php Paths
You mentioned modifying the two key lines in public_html/index.php—let’s make sure those paths are pointing correctly to your Laravel core files (the folder you created alongside public_html).
For example, if your Laravel core lives in a folder named laravel (right next to public_html), the lines should look like this:
// Replace 'laravel' with your actual folder name require __DIR__.'/../laravel/bootstrap/autoload.php'; $app = require_once __DIR__.'/../laravel/bootstrap/app.php';
If relative paths are causing headaches, use an absolute path instead (you can get your server’s absolute path via a quick phpinfo() file):
require '/home/your-username/laravel/bootstrap/autoload.php'; $app = require_once '/home/your-username/laravel/bootstrap/app.php';
2. Fix Critical File Permissions
Laravel needs write access to two directories to function properly:
storagebootstrap/cache
On shared hosts, start by setting these directories to 755 via FTP (right-click > File Permissions). If that doesn’t work, temporarily use 777 (just remember to switch back to 755 once everything is running smoothly for security).
If you have SSH access, run these commands from your Laravel root folder:
chmod -R 755 storage bootstrap/cache chown -R www-data:www-data storage bootstrap/cache # Only use this if your server uses www-data as the web user
3. Verify .env Configuration Details
Small typos here are a super common culprit—double-check these values:
DB_HOST: Some hosts don’t uselocalhost; they might give you a specific IP or socket path (e.g.,localhost:/tmp/mysql.sock).DB_DATABASE/DB_USERNAME/DB_PASSWORD: Confirm these match exactly what your host provided (they’re case-sensitive!).APP_URL: Set this to your full domain (e.g.,https://yourdomain.com).APP_DEBUG: Flip this totruetemporarily to see detailed error messages (switch back tofalsefor production!).
Also, make sure your .env file isn’t in public_html—shared hosts usually hide files starting with ., but it’s better to keep it in your Laravel core folder where it’s safe.
4. Generate a Fresh Application Key
If you copied your local .env file, the APP_KEY is tied to your local environment. You need a new key for production:
- With SSH: Run
php artisan key:generatefrom your Laravel root folder. - Without SSH: Generate a key locally with
php artisan key:generate --show, then paste the full string (includingbase64:) into your server’s.envfile.
5. Tweak .htaccess Rewrite Rules
Make sure the .htaccess file in public_html has the correct rules. Laravel’s default setup should work, but some hosts need an extra RewriteBase line to fix 404s:
<IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews -Indexes </IfModule> RewriteEngine On RewriteBase / # Add this line if you're getting 404 errors # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Send Requests To Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
Also, confirm your host has mod_rewrite enabled (check cPanel’s Apache Modules section if you use cPanel).
6. Check PHP Version & Required Extensions
Laravel 5.4 needs:
- PHP >= 5.6.4
- Extensions: OpenSSL, PDO, Mbstring, Tokenizer, XML
Create a phpinfo.php file in public_html with this content to verify:
<?php phpinfo(); ?>
Visit yourdomain.com/phpinfo.php to check the version and enabled extensions. If any are missing, reach out to your host to enable them.
7. Clear Stale Cached Files
Local cache files can cause conflicts on the server. If you have SSH access, run these commands:
php artisan cache:clear php artisan config:clear php artisan view:clear
If not, manually delete all files in:
bootstrap/cachestorage/framework/cachestorage/framework/views
内容的提问来源于stack exchange,提问作者JohnKet

