Laravel is an open source PHP framework based on the MVC (Model View Controller). Laravel makes it easier for a developer to write web applications. Laravel is considered the best framework to write PHP applications together with other frameworks. Laravel provides a modular packaging system and a dedicated dependency manager. It supports many different relational databases and provides different ways for accessing them. The Laravel framework is also very easy to install and deploy. In this tutorial we will learn to install Laravel framework on a CentOS 7.x server.
Laravel does not need any special hardware. All the required dependencies will be installed throughout the tutorial. You will only need root or sudo access on your server. If you are logged in as non root user, run
sudo -i to switch to root user or you can also use
sudo command before all administrative commands.
Before installing any package on your system, it is recommended to update the system and the packages. Run the following command to do so.
yum -y update
Now you will need to add the EPEL repository into your server as the EPEL repository has the most update packages of the dependecies required.
yum -y install epel-release yum -y update yum clean all
Now we will need to install the LAMP stack so that we can serve the Laravel web application. Install the required package for Apache and MariaDB, which is a fork of MySQL using the following command.
yum -y install httpd mariadb-server mariadb
Laravel does not supports PHP versions lower than 5.6.4, hence you will have to install a PHP version higher than 5.6.4 or you can also install PHP 7. PHP 5.6 or PHP 7 is not included in the default YUM repository, hence you will need to add Webtatic repository in your system. Run the following command to do so.
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm yum -y update yum clean all
Now you can install either PHP 5.6 or PHP 7.0 in your system. To install PHP 5.6 and all the required PHP modules, run the following command.
yum -y install php56w php56w-mysql php56w-mcrypt php56w-dom php56w-mbstring
To install PHP 7.0 and all the required PHP modules, run the following command.
yum -y install php70w php70w-mysql php70w-mcrypt php70w-dom php70w-mbstring
Make sure that you are using only one of the mentioned PHP versions. Once you have PHP installed, you can check the version of PHP using the following command.
You will likely see the following output for PHP 5.6.
[root@ip-172-31-28-226 ~]# php -v PHP 5.6.26 (cli) (built: Sep 17 2016 09:53:52) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
For PHP 7.0, you will likely see the following output.
[root@ip-172-31-28-226 ~]# php -v PHP 7.0.11 (cli) (built: Sep 17 2016 09:23:18) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
Make sure that your PHP version is not older than 5.6.4. Now start the Apache web server and enable it to start at boot time using following command.
systemctl start httpd systemctl enable httpd
To start MariaDB and enable it to start at boot time using the following command.
systemctl start mariadb systemctl enable mariadb
Now run the following command to harden your MySQL or MariaDB installation.
It will run a small script which will ask you to set a root password for your MariaDB installation. Most of the questions are self explanatory and you should answer yes for all the questions. The output of the command will look like the one shown below.
[root@ip-172-31-28-226 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.
Enter current password for root (enter for none): OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
Set root password? [Y/n] y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? [Y/n] y ... Success!
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y ... Success!
By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success!
Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reload privilege tables now? [Y/n] y ... Success!
All done! If you've completed all of the above steps, your MariaDB installation should now be secure.
Thanks for using MariaDB!
Now we will need to install Composer as Composer will help with the installation of the dependencies required by Laravel. Run the following command to download and install Composer in your system.
curl -sS https://getcomposer.org/installer | php
You will see following output.
[root@ip-172-31-28-226 ~]# curl -sS https://getcomposer.org/installer | php All settings correct for using Composer Downloading 1.2.1...
Composer successfully installed to: /root/composer.phar Use it: php composer.phar
Now run the following command to make composer available gobelally.
mv composer.phar /usr/bin/composer chmod +x /usr/bin/composer
Run the following command to check if composer is installed and working correctly.
You should see following output.
[root@ip-172-31-28-226 ~]# composer -V Composer version 1.2.1 2016-09-12 11:27:19
Now we have everything configured, we can install Laravel. The latest version of Laravel is available through the GIT repository. Run the following commands to install GIT and then switch to Apache web root directory and clone the Laravel repository in your system.
yum -y install git cd /var/www/ git clone https://github.com/laravel/laravel.git
Now navigate to Laravel files directory and use composer to install the dependencies and software.
cd /var/www/laravel composer install
Composer will take some time to install the dependencies. After composer finishes the installation, you will have to set the appropriate permissions for Laravel directory, so that it can be owned and managed by Apache.
chown -R apache:apache /var/www/laravel chmod -R 755 /var/www/laravel
The next thing we should do after installing the framework is to set the application key to a random string. It is important to set a random application key otherwise the user sessions and encrypted data will not be secured. Before generating the key it is necessary to rename
.env.example file to
.env file. Run the following command to do so.
cd /var/www/laravel mv .env.example .env
Once you have
.env file ready, you can generate the new application key using the following command.
php artisan key:generate
You will see a output similar to shown below.
[root@ip-172-31-28-226 laravel]# php artisan key:generate Application key [base64:PllGKlCqZSwTJKGyGacEN8FEJeEt0Jlyx1n8xMea3Iw=] set successfully.
The above command will generate the random application key as well as it will write the key into environment file
.env. You can verify if the key has been set using the following command.
You will see the output similar to shown below.
[root@ip-172-31-28-226 laravel]# cat /var/www/laravel/.env APP_ENV=local APP_KEY=base64:PllGKlCqZSwTJKGyGacEN8FEJeEt0Jlyx1n8xMea3Iw= APP_DEBUG=true APP_LOG_LEVEL=debug APP_URL=http://localhost
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret
BROADCAST_DRIVER=log CACHE_DRIVER=file SESSION_DRIVER=file QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379
MAIL_DRIVER=smtp MAIL_HOST=mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null
PUSHER_APP_ID= PUSHER_KEY= PUSHER_SECRET=
As you can see in the above output we have a value set for
APP_KEY, this means that the application key for our Laravel framework is set.
Now create a new Apache virtual host file so that our application can be accessed through the web browser. Edit the
/etc/httpd/conf/httpd.conf file using your favorite editor. In this tutorial we will be using
nano editor, if you do not have nano installed, you can run
yum -y install nano to install nano editor.
Now scroll down to the bottom of the file and paste these lines.
ServerName test.myproject.com DocumentRoot /var/www/laravel/public
Change the server name from
test.myproject.com according to your domain name. Now restart your Apache web server using the following command.
systemctl restart httpd
Access your the domain through the front end and you will see the application running.
In this tutorial we have learned about installing Laravel framework on CentOS 7. You can now successfully deploy a Laravel application for development purposes.