Using Apache Configuration
Ensure Apache is Installed and Running
If not already installed, install Apache and ensure it is running:
sudo apt update
sudo apt install apache2
sudo systemctl start apache2
sudo systemctl enable apache2
Enable Apache Rewrite Module
Enable the mod_rewrite module, which is necessary for URL rewriting:
sudo a2enmod rewrite
sudo systemctl restart apache2
Configure Apache Virtual Host
Modify your Apache virtual host configuration to point to Laravel’s public directory while serving the project from the root URL.
Edit the default Apache configuration file or create a new virtual host file:
sudo nano /etc/apache2/sites-available/000-default.conf
Update the <VirtualHost> block to look something like this:
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>
Set Permissions for Laravel Directories
Ensure that the necessary directories have the correct permissions:
sudo chown -R www-data:www-data /var/www/html/your-laravel-project/storage /var/www/html/your-laravel-project/bootstrap/cache
sudo chmod -R 775 /var/www/html/your-laravel-project/storage /var/www/html/your-laravel-project/bootstrap/cache
Restart Apache
Restart Apache to apply the changes:
sudo systemctl restart apache2
Verifying Your Setup
Access Your Laravel Project
Open your web browser and navigate to your EC2 instance’s public DNS:
vbnet
http://your-ec2-instance-public-dns
You should see your Laravel application’s homepage without needing to include /public in the URL.
Check Permissions and Logs
If you encounter any issues, check the Apache error logs for more details:
sudo tail -f /var/log/apache2/error.log
Additional Tips
Security: Ensure your .env file and other sensitive files are not accessible from the web. The configuration provided should protect these files, but it's good to verify.
Environment Variables: Ensure your Laravel application's environment variables are correctly set in the .env file, including the APP_URL.
0 More Answers