Introduction

Laravel is the one of the most popular and widely used open-source PHP web framework. It is known for highly expressive, speed and security. This project is developed by Taylor Otwell and is managed by the open-source community licensed under MIT License.

Ubuntu is the key player in operating system business as the Operating system for servers. It is also one of the Linux distribution and is managed by canonical Limited. We assume you have the following things ready for yourself. It is also an open source operating system licensed as free software.

Pre-requisites

  1. A server or local machine running Ubuntu server.
  2. Laravel application with separate Database Host.

Preparing the dependencies

Firstly, lets start from scratch by installing updates and upgrades for Ubuntu.

sudo apt-get update
sudo apt-get upgrade

Lets moving further and install Apache, a web server to serve the web pages for PHP application we have already built.

sudo a2enmod rewrite
sudo service apache2 restart

Deploying the application

As of now, We have successfully installed Apache package in our server. Now, We will create a site user for larasite.com site. This is an optional step but it can prove to be helpful while providing SFTP/FTP access to push/upload application codes which we will be discussing in upcoming posts.

sudo useradd -G www-data larasite

After creating the site user, we can now create a directory which holds application code and get it ready to be served by Apache. You can replace larasite with name your preferred name eg: abc.com

sudo mkdir  -p /var/www/larasite
################################################
## use www-data:www-data instead if you
## did not created larasite user in earlier step
################################################
sudo chown -R larasite:larasite /var/www/larasite
sudo chmod 755 /var/www/larasite
cd /var/www/larasite
find . -type f -print0 | xargs -0 chmod 664
find . -type d -print0 | xargs -0 chmod 775

The directories bootstrap/cache/ and storage/ requires special set of permissions. Lets manage permissions for the application code.

cd /var/www/larasite/bootstrap/
sudo chmod 777 cache/
cd cache/
find . -type f -print0 | xargs -0 chmod 666
find . -type d -print0 | xargs -0 chmod 777
cd /var/www/larasite
sudo chmod 777 storage/
cd storage/
find . -type f -print0 | xargs -0 chmod 666
find . -type d -print0 | xargs -0 chmod 777

Now, We will create a starter Apache virtual-host configuration for this site. The template for virtual host is as below:

<VirtualHost *:80>
        ServerName larasite.com
        ServerName www.larasite.com
        ServerAdmin webmaster@larasite.com
        DocumentRoot /var/www/larasite/public/
         <Directory /var/www/larasite/public/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Lets continue further by Creating a file /etc/apache2/sites-available/larasite.com.conf and paste the configuration given above for larasite.com. After we have created and saved the vhost file, we will then verify the vhost configuration for Apache. After the successful verification, disable the default vhosts and enable the new vhosts. Reload Apache service after the above steps are successful.

sudo vi /etc/apache2/sites-available/larasite.com.conf
################################################
## paste the vhost config content to the file, save and exit
################################################
sudo apache2 -t
sudo a2dissite default-000.conf
sudo a2ensite larasite.com.conf
sudo service apache2 reload

Conclusion

Finally, the application is now available using the IP address. You can hit the IP address of you machine in one of the browser in your machine for viewing the page. It can be a bit tedious to remember IP addresses.

To explore further, you can always register a Domain name for as low as $10 from one of the Domain Name registrars like DNS made easy. Point the A-record to the instance hosting the Laravel site after registering the domain name for your website. You can also create and apply SSL certificate for security. This blog is pretty much good material for creating and applying SSL certificates to your site.