wallhaven-352053


In this tutorial, you will learn to configure a local web development environment to create a project using Laravel Framework 5.3. As a plus, you will learn to configure Git and create a remote repository on GitHub. From the terminal you will install and configure:

  • Apache
  • Mysql
  • Php
  • Virtual Hosts
  • Composer
  • Laravel
  • Git

The main goal of this tutorial is to achieve a web development environment using the Linux OS and getting familiar with its required installation and configuration. So next time you find yourself setting up a web development environment (let’s say in a hosting service) it will be easier to you to keep all under control.

Although this tutorial is focused on the development of a web project using Laravel framework. All the installations and configurations shown here can be used to develop a PHP native project.

Requirements:

  • Ubuntu 16.04.1
  • GitHub account
  • Sublime Text (desirable)

Basic-intermediate experience in:

  • Apache
  • Mysql
  • Php
  • Apache Virtual Hosts (desirable)
  • Composer
  • Laravel
  • Git

Step 1: Installing Apache

Because you will using sudo command to install the required packages with root privileges, the system will prompt you regularly for your user’s password to verify your identity. Before installing Apache in the terminal execute:

sudo apt-get update

This command verifies and installs the available updates of the packages installed in the system. If you would like to see the complete list of the installed packages go to the file sources.list which is located at etc/apt. Once the updates have been installed, proceed to install Apache. In the terminal:

sudo apt-get install apache2

When the installation has completed, you will configire the server name to prevent the warning message displayed when the command bellow is executed:    

sudo apache2ctl configtest

AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message

The following command will open the file for editing using Sublime Text:

sudo subl  /etc/apache2/apache2.conf

Note: If you do not use Sublime Text you can perform its edition by using sudo nano or sudo vim commands.

To set the server’s name you can use an IP address or a domain name. For purposes of this tutorial, I’ve  used an  IP address. At the end of the file add:

ServerName 127.0.0.1

Note: If you decided to use a domain name the server’s name should look like this:

ServerName  www.example.com

Save the file and execute in the terminal:

sudo apache2ctl configtest

The below  message should be displayed:

Syntax OK

To apply the above changes restart Apache service. In the terminal:

sudo service apache2 restart

Now, open the browser and enter the server name and a screen like the one below should be displayed:

img_12.jpg

Step 2: Installing Mysql

In the terminal  execute:

sudo apt-get install mysql-server

Configuring Mysql Server 5.7

During the installation, Mysql will prompt you several times to establish and confirm root’s password. When the installation has completed verify that it was successfully installed by login in as root user. In the terminal execute:

mysql -u root -p

The follow message should be displayed:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.15-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql> 

If the installation was not successfully installed I would recommend you to remove it and start again. To do so in the terminal:

1.- Delete Mysql Server package and its configuration.

sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.5 Mysql-client-core-5.5

2.- Delete Mysql folder and its content:

sudo rm -rf /etc/mysql /var/lib/mysql

3.- Delete package dependencies:

sudo apt-get autoremove

4.- Delete Mysql package files from the system cache:

sudo apt-get autoclean

Step 3: Installing PHP

In the terminal  execute:

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

When the installation has completed, instruct  Apache to look first for an index.php file instead of the index.html in each request. You will modify the dir.conf file, in the terminal:

sudo subl /etc/apache2/mods-enabled/dir.conf

The default file content looks like this:

<IfModule mod_dir.c>
   DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Place index.php at the beginning:

<IfModule mod_dir.c>
   DirectoryIndex  index.php index.cgi index.pl index.html index.xhtml index.htm
</IfModule>

To apply the changes, save the file and restart Apache service. In the terminal:

sudo service apache2 restart

By last verify that PHP has been configured correctly by doing:

1.- Create a file in web root folder (www/html) called info.php.

sudo touch /var/www/html/info.php

2.-  Open the file:

sudo subl /var/www/html/info.php

3.- Edit the file and use the PHP’s native function phpinfo()  to display its configuration. The file should looks like below:

PHP info function

4.- Save and close the file.

5.- In your browser open this file ,  a screen like  below should be displayed:

PHP info

Once verified PHP configuration delete phpinfo.php file:

sudo rm /var/www/html/info.php

Step 4: Creating and configuring a Virtual Host Name for the project

Project’s Folder

Before starting to create and configure the Virtual Host of the project, create a folder to host its files. For purposes of this tutorial, it will be named laravel-project but feel free to name it as you need. In the terminal:

sudo mkdir -p  /var/www/laravel-project

User and folder permissions

Once created,  grant read and write permissions to your user over the folder:

sudo chown -R username:username /var/www/laravel-project

. Grant the following permissions to the www folder by running this command:

sudo chmod -R 755 /var/www 

Virtual Host: File creation

Apache already comes with a default file 000-default.conf located at:

/etc/apache2/sites-available/000-default.conf

Create a new file based on this one.  I’ve named it laravel-project.com.conf but feel free to name it as you need. In the terminal:

sudo cp /etc/apache2/sites-available/000-default.conf  /etc/apache2/sites- available/laravel-project.com.conf

IMPORTANT: Apache requires that each Virtual Host file has the .conf extension.

Virtual Host: Configuration

Open the file and edit:

sudo subl /etc/apache2/sites-available/laravel-project.com.conf

Virtual Host default file, Ubuntu 16.04.1

In this example, I’ve set the server’s name as laravel-project.dev but feel free to name it as you need.

Server Name: Sets the Virtual Host Name and the port the site will respond to, basically behaves like a domain.

Server Alias: Sets alternative names with which a Virtual Host can be identified.

Document Root: Indicates the path of the project’s root folder which will be linked to the Virtual Host.

Directory: Sets a group of rules that will be applied to the  Virtual Host folder and its subfolders.

Options: This sentence helps us to configure the available features in the Virtual Host’s  folder. By default, it is set as Options All but we can use + to add and – to remove features. You will set the below features:

-Indexes: Prevents mod_autoindex from returning the list with the contents of the directory when there is no DirectoryIndex (example index.html).

+ FollowSymLinks: Indicates to the server to follow (or not ) the symbolic links present in the directory.

-MultiViews: Prevents that the site is displayed according to the browser’s preferences (e.g, media type, languages, document encoding, etc).

AllowOverride All: This option is only available for the sections and indicates the instructions allowed in the .htaccess files. For this example, you will indicate that all types of instructions are allowed.

Require All Granted: Imitates the Allow All functionality.

Error Log: Sets the filename where the server will register the errors found.

Access Log: Sets the filename that will register all requests processed by the server.

The Virtual Host file should look as below:

Virtual Host laravel-project.com.conf

Here is listed the laravel-project.com.conf  Virtual Host above:

Enabling the Virtual Host

In the terminal:

sudo a2ensite laravel-project.com.conf

The system will ask  you to reload Apache server but to ensure  that changes are applied correctly restart it:

sudo service apache2 restart

IMPORTANT: If you decide to not longer use this configuration file, don’t just delete it, first you have to disable it because once the virtual host has been enabled Apache will search for it within sites-availables folder. To disable a virtual host in the terminal:

sudo a2dissite  laravel-project.com.conf

Once disabled it you can proceed to delete the file.

Add the Virtual Host to the System’s hosts file

Adding the Virtual Host to the System’s hosts file allows us to access the website either by entering the server name or alias established. In this example laravel-project.dev . Open the hosts file in the terminal:

sudo subl /etc/hosts

At the end of the file add the server name or alias:

127.0.0.1  laravel-project.dev|

Note: The server name/alias only will work on your computer because is linked to the machine’s loopback IP 127.0.0.1

Step 5: Composer

Before installing Composer check if the required dependencies of Curl and Git are already installed. In the terminal:

curl –version

If you get a message like this it means that curl’s dependency is already installed:

curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp

Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets

git –version

If you get a message as below it means that Git is already installed.

git version 2.7.4

If none of the previous messages are shown download and install them. In the terminal:

Curl installation

sudo apt-get install curl php7.0-cli git

Composer installation

curl -sS https://getcomposer.org/installer | sudo php — –install-dir=/usr/ local/bin –filename=composer

Once the installation has finished, execute:

composer

The below message should be displayed:

img_42

Step 6: Installing Laravel Framework

Laravel’s official documentation tells us we need the following requirements for any Laravel project:

  • PHP >= 5.6.4
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

Before verifying these requirements, enable the a2enwrite module which allows rewriting the dynamic URLs that will be created in the application, which is tipically needed by MVC frameworks.

sudo a2enmod rewrite

If the module has been activated correctly the system will prompt you to restart the apache server:

sudo service apache2 restart

Checking  Laravel’s Server requirements:

1.- PHP’s version, to get the PHP version,  in the terminal:

php -v

2.- Listing the installed PHP modules

php -m

Within the list look for the following modules:

  • openssl
  • pdo_php
  • mbstring
  • tokenizer
  • xml

To do so you  have two options: Scroll over the list or search the module by its name using the  command:

php -m | grep  moduleName

If any of the modules is not listed, you have to install it. Use the below command to do so:

sudo apt-get install php7.0-moduleName

Or if you prefer you can install as well several modules at once using:

sudo apt-get install php7.0-moduleName  php7.0-moduleName

E.g

sudo apt-get install php7.0-mbstring   php7.0-xml

If you would like to know which are the Apache active modules use the command:

sudo apachectl -M

Installing Laravel Framework via Composer

Once you have ensured that all the modules are installed, proceed to install Laravel via Composer with the following command:

composer create-project –prefer-dist laravel/laravel .

Note: The period at the end of the command is used to avoid the creation of the laravel-project folder we already have. It is very likely that  after the project has been created some messages  might appear, feel free to ignore them:

symfony/vardumper suggests installing ext-symfony debug ()……

As the official documentation indicates you have to grant read and write permissions over storage and boot/cache folders.  You can achieve this by entering the below commands:

sudo chmod -R o+w storage/

sudo chmod -R o+w bootstrap/cache

 Finally,  generate the application key. In the terminal:

php artisan key:generate

Open the browser and enter the virtual host name or alias. The Laravel home page should be displayed, if not I highly recommend you to review very carefully each of the previous steps but if you are still having issues let me know at the comments section.

Laravel-5.3

Step7: Configuring Git

Previously you have installed Git but you haven’t configured it.  Since Git uses the username and the email within the commits you will set them in the global configuration.

Git: Global configuration

Use the command below to set the username:

git config –global user.name “Selene Arzola”

Use the command below to set the email:

git config –global user.email “helloselene@example.com

Additionally, indicate to git to use Sublime Text when you  need to edit git files:

git config –global core.editor “subl -n -w”

The -n and -w parameters instruct to Sublime Text to be executed in a new window and wait until the user closes the window to return the callback from where it  was called, in this case Git. 

So far so good. List  the global configuration  using the command below:

git config –list

Git: Generating SSH key

Verify if a key already exists. In the terminal:

ls -al ~/.ssh

If there are no keys created, a message like this will be displayed:

ls: cannot access ‘/home/helloselene/.ssh’: No such file or directory

Use the command below to generate a SSH key:

ssh-keygen -t rsa -b 4096 -C “helloselene@example.com”

When you’re prompted to enter a file location in which to save the key, press Enter since is not mandatory to specify one. By default, the key is saved in the home directory.

Enter file in which to save the key (/home/helloselene/.ssh/id_rsa):

It will also ask for a phrase (and its confirmation)  for the key’s encryption. For the purpose of this tutorial, we are not establishing one. But feel free if you want to. Press enter twice. When the creation has finished a message like follow will be displayed:

Your identification has been saved in /home/helloselene/.ssh/id_rsa.
Your public key has been saved in /home/helloselene/.ssh/id_rsa.

+—[RSA 4096]—-+
|    . .Bo.       |
|   . .*.+ o      |
|    .Eo+.*       |
|  . oo..O+o      |
|.. *  +*S+.      |
|..o oo+o..       |
|.. +.. =.        |
| .=.o o ..       |
| .oo . ….      |

Git: Adding SSH key to GitHub account

1.- Copy to the clipboard the contents of the created key.

2.- Go to your GitHub account upper right corner ->profile photo->settings.

3.- In the user’s  configuration side menu click the SSH and GPG keys -> click new key.

GitHub recommends us to provide in the title’s input the computer’s name where the SSH key was created.

4.- Paste the key in the key’s input and click add the key. You will be prompted to provide your account’s password to confirm this action. You will receive an email notification telling you have added a new key.

Git: Adding the project files to git and commit

Initialize Git in  your project, to do so in the command line browse to the project’s folder and enter the command below:

git init

Add to Git all the project files and commit:

git add .

git commit -m  “Laravel setup”

Note: The -m option refers to the commit’s  message. If you leave out the -m option, Git will try to open the default text editor for you to write your commit message.

Git: Creating and configuring a remote (public) repository in GitHub

1.- In the GitHub page upper right corner click the plus sign -> create a new repository.

2.- Provide the repository name and description. Click create.

3.- Once the repository has been created Github will redirect you to the repository’s configuration page. Look for the add an existing repository option and follow the instructions to add the created remote repository to our project and update it with our local changes.

This is how we conclude with the configuration of our development environment, now our project is ready to be developed.

Did you like the tutorial? I would like to know your opinion, please let me know in the comments section.

Advertisements