Achmad Dinofaldi Firmansyah
Posted on June 29, 2023
Are you looking to deploy your Laravel application on an EC2 instance in AWS? In this tutorial, we will walk you through the step-by-step process of deploying Laravel to EC2 using Apache as the web server. Let's get started!
Step 1: Install PHP with Apache2
First, we need to install PHP and Apache on our EC2 instance. Connect to your EC2 instance using SSH and run the following commands:
sudo apt-get update
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt -y install php7.4
php -v
sudo apt-get install -y php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath
php -m
This step installs PHP and Apache web server on your EC2 instance. It ensures that you have PHP installed with the necessary extensions required by Laravel.
Step 2: Install Composer
Composer is a dependency management tool for PHP. We need to install Composer to manage the dependencies of our Laravel application. Run the following commands:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer
Composer is a dependency management tool for PHP. This step installs Composer on your EC2 instance. Composer will be used to manage the dependencies of your Laravel application.
Step 3: Install Laravel or Clone Laravel from Git
Next, navigate to the /var/www directory and either install Laravel using Composer or clone an existing Laravel project from Git. Here are the commands:
cd /var/www
# Install Laravel using Composer
composer create-project --prefer-dist laravel/laravel your-project-name
# OR
# Clone Laravel project from Git
git clone git@github.com:yourusername/your-repo.git your-project-name
In this step, you navigate to the /var/www directory, which is the default web directory for Apache on Ubuntu. You have two options:
- Install Laravel using Composer: This command creates a new Laravel project in the your-project-name directory using the laravel/laravel package.
- Clone Laravel project from Git: This command clones an existing Laravel project from a Git repository into the your-project-name directory.
Replace your-project-name with the desired name for your Laravel project.
Step 4: Configure the Environment
After installing Laravel or cloning the project, you need to configure the environment. The .env.example file is copied to .env. You should edit the .env file and set up the appropriate database configuration for your environment.
Copy the .env.example file to .env using the following command:
cp /var/www/your-project-name/.env.example /var/www/your-project-name/.env
Edit the .env file and set up your database configuration according to your environment.
Generate a unique application key by running the following command:
cd /var/www/your-project-name
php artisan key:generate
The php artisan key:generate command generates a unique application key, which is used for encryption and other security-related purposes in Laravel.
Step 5: Database Migration
Laravel provides a migration system to manage database schema changes. The php artisan migrate command runs the database migrations and creates the necessary tables in your database based on the migration files defined in your Laravel project.
Run the database migrations to create the necessary tables in your database:
php artisan migrate
Step 6: Set File and Folder Permissions
To ensure that your Laravel application has the correct file and folder permissions, run the following commands:
sudo chown -R www-data:www-data /var/www/your-project-name
sudo find /var/www/your-project-name -type f -exec chmod 644 {} \;
sudo find /var/www/your-project-name -type d -exec chmod 755 {} \;
cd /var/www/your-project-name
sudo chown -R $USER:www-data .
sudo find . -type f -exec chmod 664 {} \;
sudo find . -type d -exec chmod 775 {} \;
This step ensures that the appropriate file and folder permissions are set for your Laravel project. It ensures that the web server (usually www-data user) has the necessary permissions to access and modify files.
The commands in this step set the ownership and permissions recursively for the Laravel project directory.
Step 7: Configure Apache2
Apache needs to be configured to serve your Laravel application. This step involves creating a new Apache configuration file specific to your project.
Create a new Apache configuration file for your Laravel project:
sudo nano /etc/apache2/sites-available/your-project-name.conf
Paste the following configuration into the file:
<VirtualHost *:80>
ServerAdmin admin@hwdomain.io
ServerName laravelapp.hwdomain.io
DocumentRoot /var/www/your-project-name/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/your-project-name>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Replace your-project-name with the actual name of your Laravel project. The ServerName directive should match your desired domain or subdomain.
The configuration file sets the ServerName directive to the desired domain or subdomain for your application. The DocumentRoot points to the public directory of your Laravel project.
Create a symbolic link for the configuration file in the Apache sites-enabled directory:
sudo ln -s /etc/apache2/sites-available/your-project-name.conf /etc/apache2/sites-enabled/your-project-name.conf
Additionally, the configuration enables the necessary overrides and logging for your Laravel application.
Step 8: Restart Apache
Finally, you need to restart the Apache web server for the configuration changes to take effect. This ensures that your Laravel application is now being served by Apache and can be accessed using the specified domain or subdomain.
sudo service apache2 restart
That's it! Your Laravel application is now deployed on an EC2 instance in AWS. You should be able to access it using the specified domain or subdomain.
Remember to configure any additional settings such as DNS records or SSL certificates to make your application accessible over the internet securely.
Congratulations on successfully deploying your Laravel application on EC2 AWS! Happy coding!
Posted on June 29, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.