1. Ensure Apache is Installed and Running
First, make sure that Apache is installed and running on your EC2 instance.
sudo apt update
sudo apt install apache2
sudo systemctl start apache2
sudo systemctl enable apache2
2. Enable the mod_rewrite Module
Laravel relies on URL rewriting to route requests. Ensure the mod_rewrite module is enabled.
sudo a2enmod rewrite
sudo systemctl restart apache2
3. Configure the Virtual Host to Allow Overrides
Your Apache configuration must allow .htaccess files to override settings. Edit the default Apache virtual host configuration or the specific virtual host configuration for your Laravel site.
sudo nano /etc/apache2/sites-available/000-default.conf
Update the configuration to allow overrides in the public directory:
apache
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/your-laravel-project/public
<Directory /var/www/html/your-laravel-project/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save the file and exit the editor.
4. Set the Correct Permissions
Ensure that the public directory and .htaccess file have the correct permissions.
sudo chown -R www-data:www-data /var/www/html/your-laravel-project/public
sudo chmod -R 755 /var/www/html/your-laravel-project/public
5. Check the .htaccess File
Ensure your .htaccess file in the public directory is correctly configured. It should look something like this:
apache
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes...
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>
6. Restart Apache
After making all the changes, restart Apache to apply the new configurations.
sudo systemctl restart apache2
7. Verify the Configuration
After completing the above steps, verify that your Laravel application is working correctly without needing to specify public in the URL. Open your web browser and navigate to your site’s domain or public DNS.
http
http://your-ec2-instance-public-dns
Example Commands to Execute
Here’s a summary of the commands to execute in sequence:
# Connect to your EC2 instance
ssh -i /path/to/your-key.pem ubuntu@your-ec2-instance-public-dns
# Ensure Apache is installed and running
sudo apt update
sudo apt install apache2
sudo systemctl start apache2
sudo systemctl enable apache2
# Enable Apache mod_rewrite
sudo a2enmod rewrite
sudo systemctl restart apache2
# Configure Apache virtual host
sudo nano /etc/apache2/sites-available/000-default.conf
# (Update the configuration as shown above, then save and exit)
# Set correct ownership to www-data
sudo chown -R www-data:www-data /var/www/html/your-laravel-project/public
# Set permissions to 755
sudo chmod -R 755 /var/www/html/your-laravel-project/public
# Ensure the .htaccess file is correctly configured
sudo nano /var/www/html/your-laravel-project/public/.htaccess
# (Ensure it has the correct content as shown above, then save and exit)
# Restart Apache
sudo systemctl restart apache2
0 More Answers