Host Laravel Application on Amazon EC2

We’ll break down the process in small steps.

  1. Create an Account on AWS & go to EC2 service Page. Launch new instance & Login to your server using shell.

  2. We’ll run this command to update the list of app available on linux cloud sudo apt update

  3. Install Apache Webserver with this command sudo apt install apache2. You can check the status of your apache server using this command sudo systemctl status apache2. This should reply with “Active: active(running)”.

  4. Allow access on http ports through firewall by this command sudo ufw allow Apache Full

At this point you should be able to access your website on port 80 using IP address. Check your webserver’s ip address & put it in your web browser eg: http://192.168.10.10. You should see the default apache page.

  1. Let’s install git with this command sudo apt install git

  2. We’ll create public/private key pair for this server. Throw this command ssh-keygen, Press enter twice when it asks you for input. This will create two key files inside ~/.ssh folder.

  3. Change current directory to /var/www where all the website code is stored. You can run this command cd /var/www/

  4. There must already be a folder called html. We’ll create new folder for our new site: sudo mkdir laravel-demo & CD into that folder.

  5. We’ll pull our codebase from github: git clone git@github.com:laravel/laravel.git . :information_source: Notice the use of DOT. It tells git to copy contents in current working directory without creating new directory.

  6. Install PHP sudo apt-get install php7.2 php7.2-cli php7.2-common php7.2-zip php7.2-xml

  7. Let’s install composer now. Bear with me because we’ll have to run few commands.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
  1. We’ll now headover to our project folder (cd /var/www/laravel-demo) & try to install our composer dependencies. sudo composer install

  2. Now this is the big moment. We’ll create VHost On apache. sudo nano /etc/apache2/sites-available/example-domain.conf This will open an editor. Copy the following code & paste it in the terminal.

Remember to make necessary changes to these files if you have named your folder differently.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName your_domain
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

You can exit out of this terminal by pressing these combo ctrl + x followed by Y.

sudo a2dissite 000-default.conf
sudo systemctl restart apache2
  1. Head over to your domain registrar & make DNS “A” type record. With following details:
Value: your_public_ip_address from AWS->EC2 Service Page
TTL: 43200
  1. Install Let’s Encrypt’s free SSL tool. It will help us setup SSL on our website.
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot python-certbot-apache
sudo certbot --apache

When prompt appears: Select 1. if you want all traffic on your website to go to HTTPS version.

I think that’s about it. If you’re stuck somewhere please feel free to reach out to me on twitter @izshreyansh.