Install Laravel on CentOS 6.X

You can easily install Laravel on a LAMP stack running on a CentOS 6.9 within less than an hour. For this Laravel install tutorial we are using a clean install of CentOS 6.9 on a Face VPS 1 package, however you can install it also on any Dedicated Server machine as well.

First, we will login via SSH as the root user:

ssh root@{IP_ADDRESS}

We’ll update the Operating System and all currently installed packages

# Updating the basic VPS’ packages

yum update

We’re acquainted with VIM so we’ll use it

# Installing VIM for text editor

yum install vim

In order to run Laravel on the VPS, we’ll need Apache, MySQL and PHP, the setup that we’ll show is using Apache 2.2, MySQL 5.6 and PHP 7.1 directly configured from Remi’s Repository.

# Installing Remi’s Repository

yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

After that, we’ll install the basic packages required to run a fresh Laravel installation

# Basic setup – Git, Apache, MySQL, PHP & Extensions

yum --enablerepo=remi,remi-php71 install git httpd php php-common mysql mysql-server php-pecl-zip php-mysql php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

We’ve chosen to install Laravel using Composer, so we’ll need to set it first:

# Installing Composer

wget https://getcomposer.org/composer.phar
chmod +x composer.phar
mv composer.phar /usr/bin/composer

We’re ready to install Laravel using Composer. We’ll set the installation path to be the default /var/ww/html and for testing purposes (or lack of imagination) we’ll use example.com for domain

cPanel Hosting from WebhostFace

# Creating a directory and making the installation:

cd /var/www/html/

mkdir example.com
composer create-project --prefer-dist laravel/laravel example.com/

When this is done we’ll need to change the permissions so that Apache has access to write logs. In addition, we’ll also make a custom apache_logs folder for error and access logs.

# Default Apache Logs

mkdir example.com/apache_logs

# Fixing ownership

chown -R apache:apache example.com/*

Now we’ll need to instruct Apache to start responding to requests made for that domain. For this, we’ll use Named Virtual Hosts and we’ll also set a Virtual Host for our domain – example.com

# Enabling and adding a Virtual Host

vim /etc/httpd/conf/httpd.conf

# Append the following at the end of the file (note that we’re using example.com as domain and folder)

NameVirtualHost *:80

<VirtualHost *:80>

ServerAdmin webmaster@ example.com

DocumentRoot /var/www/html/example.com/public/

ServerName example.com

ServerAlias www. example.com

<Directory /var/www/html/example.com/>

Options FollowSymLinks

AllowOverride All

Order allow,deny

allow from all

</Directory>

ErrorLog /var/www/html/example.com/apache_logs/error_log

CustomLog /var/www/html/example.com/apache_logs/access_log common

</VirtualHost>

After that we’ll need to start MySQL, create our Database and secure access to it:

# Starting MySQL and using it’s CLI to create a Database called “laravel”

service mysqld start

mysql

CREATE DATABASE laravel COLLATE utf8_general_ci DEFAULT CHARACTER SET utf8;

# Secure MySQL access:

mysql_secure_installation

When we’re ready with our Database we’ll need to adjust it in Larave’s .env configuration file:

# Adjusting DB Settings

vim config/database.php
'mysql' => [
    'read' => [
        'host' => [
            '192.168.1.1',
            '196.168.1.2',
        ],
    ],
    'write' => [
        'host' => [
            '196.168.1.3',
         ],
    ],
    'sticky'    => true,
    'driver'    => 'mysql',
    'database'  => 'laravel',
    'username'  => 'root',
    'password'  => 'THE ONE YOU CREATED THE USER WITH',
    'charset'   => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix'    => '',
],

We should be done with our basic Laravel setup, now we just have to start Apache

service httpd start
service mysqld start

After that Laravel will be served if the domain is pointed towards the VPS. For this, we could use CloudFlare for DNS and simply change the A record over there to be the VPS’ primary IP address.