To deploy a Laravel application on an AWS EC2 Ubuntu 22.04 server, you can follow the below steps:
- Create an EC2 instance: First, create an EC2 instance in your AWS account with Ubuntu 22.04 as the operating system.
- Connect to your EC2 instance: After the instance is created, connect to it via SSH.
- Update the server: Run the following commands to update your server:
1 2 |
sudo apt-get update sudo apt-get upgrade |
Install Apache web server: Run the following command to install Apache web server:
1 |
sudo apt-get install apache2 |
Install PHP: Run the following command to install PHP:
1 |
sudo apt-get install php |
Install necessary PHP extensions: Run the following command to install necessary PHP extensions:
1 |
sudo apt-get install php-{bcmath,bz2,intl,gd,mbstring,mysql,zip,fpm} |
Install MySQL: Run the following command to install MySQL:
1 |
sudo apt-get install mysql-server |
Install Git: Run the following command to install Git:
1 |
sudo apt-get install git |
Clone the Laravel application: Clone the Laravel application from your Git repository to the /var/www directory using the following command:
1 |
sudo git clone <git-repo-url> /var/www/<laravel-app-name> |
Set permissions: Set the correct permissions for the Laravel application directory and storage directory:
1 2 |
sudo chown -R www-data:www-data /var/www/<laravel-app-name> sudo chmod -R 755 /var/www/<laravel-app-name>/storage |
Configure Apache: Create a new Apache virtual host configuration file by running the following command:
1 |
sudo nano /etc/apache2/sites-available/<laravel-app-name>.conf |
Add the following configuration to the file:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<VirtualHost *:80> ServerAdmin webmaster@mywebsite.com ServerName mywebsite.com ServerAlias www.mywebsite.com DocumentRoot /var/www/<laravel-app-name>/public <Directory /var/www/<laravel-app-name>/public> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> |
Save and exit the file.
Enable the new virtual host: Run the following command to enable the new virtual host:
1 |
sudo a2ensite <laravel-app-name>.conf |
Restart Apache: Run the following command to restart Apache:
1 |
sudo service apache2 restart |
Configure the .env file: Create a new .env file by running the following command:
1 |
sudo cp /var/www/<laravel-app-name>/.env.example /var/www/<laravel-app-name>/.env |
Edit the .env file and set the appropriate values for your application’s database and other configuration settings.
Run migrations: Run the following command to run the Laravel migrations:
1 2 |
cd /var/www/<laravel-app-name> php artisan migrate |
Your Laravel application should now be deployed and running on your AWS EC2 Ubuntu 22.04 server. You can access it by visiting your server’s public IP address or domain name in your web browser.