Step-by-Step Guide
1. Ensure the Web Server Points to the Correct Directory
Laravel projects should serve from the public directory. Configure your web server to point to this directory.
For Apache:
Open the Apache configuration file:
sudo nano /etc/httpd/conf/httpd.conf # For Amazon Linux
sudo nano /etc/apache2/sites-available/000-default.conf # For Ubuntu
Change the DocumentRoot to the public directory of your Laravel project:
apache
Copy code
DocumentRoot "/path/to/your/laravel/public"
<Directory "/path/to/your/laravel/public">
AllowOverride All
Require all granted
</Directory>
Save and close the file.
Enable the mod_rewrite module if it isn't already enabled:
sudo a2enmod rewrite # For Ubuntu
sudo systemctl restart apache2 # Restart Apache to apply changes
sudo systemctl restart httpd # For Amazon Linux
For Nginx:
Open the Nginx configuration file:
sudo nano /etc/nginx/nginx.conf # For Amazon Linux
sudo nano /etc/nginx/sites-available/default # For Ubuntu
Update the server block to point to the public directory of your Laravel project:
nginx
Copy code
server {
listen 80;
server_name your_domain.com;
root /path/to/your/laravel/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf; # For Ubuntu
include fastcgi_params; # For Amazon Linux
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust PHP version as needed
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Save and close the file.
Test the Nginx configuration:
sudo nginx -t
Restart Nginx to apply the changes:
sudo systemctl restart nginx
2. Ensure Laravel Permissions Are Correct
Make sure the storage and cache directories are writable:
cd /path/to/your/laravel
sudo chown -R www-data:www-data storage bootstrap/cache # For Ubuntu
sudo chown -R apache:apache storage bootstrap/cache # For Amazon Linux
sudo chmod -R 775 storage bootstrap/cache
3. Ensure .env File Is Correct
Verify that the .env file is correctly configured with your database and other settings.
4. Clear Laravel Cache
Clear and optimize Laravel caches:
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
5. Ensure Composer Dependencies Are Installed
Make sure all dependencies are installed:
cd /path/to/your/laravel
composer install --optimize-autoloader --no-dev
0 More Answers