Installing Laravel PHP Framework on Ubuntu for Apache 2016

Pre-Requisities

Before proceeding with the installation, its always a good idea to make sure your sources and existing softwares are all updated. 
sudo apt-get update
sudo apt-get upgrade
For this guide we will assume that you have a basic server based on Ubuntu running. Before Laravel, we need to install other components that are essential.

Installing PHP5

Next step is to install PHP5 along with several extra packages that would prove useful if you are going to work with Laravel. 
sudo add-apt-respository ppa:ondrej/php5
sudo apt-get update
sudo apt-get install php5 php5-mcrypt php5-gd
sudo php5enmod mcrypt
Even though Ubuntu's own repository has PHP, its better to add a 3rd party repository here because it gets more frequently updated. You can skip that step and stick to Ubuntu's version if that's what you prefer.

Installing Apache

Its time to install Apache server now. We would also need to install libapache2-mod-php5 package to hook up Apache with PHP.
sudo apt-get install apache2 libapache2-mod-php5

Installing Laravel

Before we finally delve into it, we also need Git version control to be installed. If you have it installed, you can skip the following step. If you don't have, then you can follow our guide to set it up first.
To install Laravel, we need to install Composer first. It is a tool for dependency management in PHP that allows you to package all the required libraries associated with a package as one. To install Laravel and all its dependencies, Composer is required. It will download and install everything that is required to run Laravel framework. To install Composer, issue the following commands.
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
First command downloads composer.phar package to your directory. But we would want composer to run globally hence we need to move it to /usr/local/bin directory under the name 'composer'. Now we can run composer from anywhere.
To install Laravel, move to the public html directory on your system. Since we are on Ubuntu and using Apache, we would install it in /var/www/html directory.
cd /var/www/html
sudo composer create-project laravel/laravel your-project --prefer-dist
The above command will create a your-project directory with laravel installation in it. Composer uses git to download and install all the packages and modules that Laravel requires for functioning.

Configuring Apache

Now that we have installed Laravel, we move on to the step of configuring Apache webserver.
Next step is to give proper permissions to the project directory. For this we need to enable access to it from the www-data group and to give it writing permissions to the storage directory.
sudo chgrp -R www-data /var/www/html/project
sudo chmod -R 775 /var/www/html/project/storage
Now go to the /etc/apache2/sites-available directory and use the following command to create a configuration file for our laravel install.
cd /etc/apache2/sites-available
sudo nano laravel.conf
Now add the following content to the file and close it after saving.
<VirtualHost *:80>
    ServerName localhost

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/project/public

    <Directory /var/www/html/project>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now we have to enable this newly created .conf file and disable the default .conf file that is installed with the default Apache install. Also we need to enable mod_rewrite so that permalinks can function properly.
sudo a2dissite 000-default.conf
sudo a2ensite laravel.conf
sudo a2enmod rewrite
sudo service apache2 restart

Conclusion

Your Laravel installation is now complete. Visit http://serverip or http://localhost to access your Laravel installation. If successful you should see the following screen.

No comments :