Installing Laravel 5.5 on a LAMP stack in Ubuntu 18.04

12 May 2018

Install Apache HTTP Server

sudo apt install apache2

Install PHP version 7.2 and some required packages

    sudo apt install php7.2 php7.2-xml php7.2-mbstring

Install MySQL

sudo apt install mysql-server

Run through an initial setup to lockdown MySQL, if desired

sudo mysql_secure_installation

We’re not actually going to set up a database and connect our Laravel application to it, but you’re probably going to want to be able to do that. I may cover it in a separate tutorial

Install curl so we can get the composer installation package

sudo apt install curl

cd /tmp

Get composer

curl -sS https://getcomposer.org/installer -o composer-setup.php

Install composer globally

sudo php composer-setup.php --install-dir=/usr/bin --filename=composer

cd /var/wwww/

Change ownership of the html directory where “user” is your username, and the group is “www-data”

sudo chown user:www-data html

cd html

Create the “your-project” directory and install Laravel in it

composer create-project laravel/laravel your-project --prefer-dist

Change the group of all files in “your-project” to www-data

sudo chgrp -R www-data your-project

Set required permissions for the directory “storage” to 775

sudo chmod -R 775 your-project/storage/

cd /etc/apache2/sites-available/

Copy the default Apache configuration file to use for your-project

sudo cp 000-default.conf your-project.conf

Open your-project.conf, with super-user privileges, using the text editor of your choice. You should see the following:


<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Uncomment “ServerName” and set the domain. ex: ServerName www.your-project.com

Change “DocumentRoot” to /var/www/html/your-project/public

Add the following:


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

Save the file, and it should now look like this:


<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName www.your-project.com

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

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

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Now enable the new configuration

sudo a2ensite your-project.conf

Enable Apache’s mod_rewrite

sudo a2enmod rewrite

And now restart Apache to load the new configuration

sudo service apache2 restart

Now we’re going to edit the hosts file so that we can view our locally installed laravel application, through a web browser

Edit /etc/hosts using your text editor of choice, and add a new line with the following:

127.0.0.1 www.your-project.com

Save the file and go to www.your-project.com in your web browser…

laravel-fresh-install

SUCCESS

Categories: