How to Install Laravel 6 on Red Hat

How to Install Laravel 6 on Red Hat

Posted on

Laravel is of the famous PHP framework for artisans that used by many web developer and have huge community. It also evolves faster because of it. Recently, Laravel release its major version, Laravel 6 on September 3rd 2019. This major version is also released as LTS (Long Term Support) that will get bug fixes update until September 3rd, 2021 and sccurity fixes until September 3rd, 2022. For details information about released version you can navigate to Laravel Release Notes.

Now, we are going to install Laravel 6 in step by step. Our environments using Red Hat 7.5, you can also install to other OS with similar steps.

Step 1. System Requirements

The Laravel framework has a few system requirements. All of these requirements are satisfied by the Laravel Homestead virtual machine, so it’s highly recommended that you use Homestead as your local Laravel development environment.

However, if you are not using Homestead, you will need to make sure your server meets the following requirements:

  • PHP >= 7.2.0
  • BCMath PHP Extension
  • Ctype PHP Extension
  • JSON PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

Step 2. Create User

With root login create a new user to run laravel application.

adduser laravel
usermod -aG wheel laravel
su - laravel

Step 3. Install Web Server

There are some famous web server out there, you can use Apache or Nginx.

Apache

To install Apache web server:

sudo yum install httpd

Now we can enable and start Apache service:

# sudo systemctl enable httpd
# sudo systemctl start httpd
Redirecting to /bin/systemctl start  httpd.service

NginX

To install NginX web server, you need to add Nginx repository and install from there.

vi /etc/yum.repos.d/nginx.repo

Append with below code.

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

Save by pressing Esc followed by :wq and hit Enter

Install Nginx.

sudo yum install nginx

Enable and start Nginx.

sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx

Step 4. Install PHP and modules needed.

Install PHP modules required.

sudo yum install php php-common php-bcmath php-mbstring php-pdo php-mysql php-tokenizer php-zip php-gd php-phar php-xml php-cli php-openssl -y

Restart nginx or apache2.

#restart nginx
sudo systemctl restart nginx
#or apache
sudo systemctl restart httpd

Step 5. Install Database

We use MariaDB to save our databases.

Add a repository file and name it i.e. MariaDB.repo inside /etc/yum.repos.d/ and append with this repo code.

# MariaDB 10.4 RedHat repository list - created 2019-10-27 23:43 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/rhel7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Update yum cache and accept MariaDB GPG key if asked.

sudo yum update

After that, install MariaDB with below command.

sudo yum install MariaDB-server MariaDB-client

It is important to secure the server. MariaDB is shipped with a script to harden the system accordingly using a user dialog. It is highly recommended to use this script before creating and populating the databases.

Run mysql_secure_installation.

sudo mysql_secure_installation

Enter current password for root (enter for none): ENTER
Set root password? [Y/n] y
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

Create a database for laravel projects.

sudo mysql -u root -p
 
CREATE DATABASE laravel;
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON laravel.* TO 'laravel'@'localhost';
FLUSH PRIVILEGES;
exit;

Step 6. Install Composer

Composer is a package manager for PHP. Its function similar to package manager in Linux distros such as in Ubuntu, if you need to update the script, manage dependency, composer will handle that for you.

Install Composer.

sudo yum install unzip curl -y
cd /tmp
curl https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer	

Step 7. Install Laravel via Composer

If you have no swap directory in your server than make one.

sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo /sbin/mkswap /var/swap.1
sudo /sbin/swapon /var/swap.1
sudo chmod 600 /var/swap.1	

Make project via Laravel installer

composer global require laravel/installer

Make sure to place composer’s system-wide vendor bin directory in your $PATH so the laravel executable can be located by your system. This directory exists in different locations based on your operating system.

Once installed, the laravel new command will create a fresh Laravel installation in the directory you specify. For instance, laravel new blog will create a directory named blog containing a fresh Laravel installation with all of Laravel’s dependencies already installed.

Create a laravel project named laravel.

sudo mkdir /var/www/
cd /var/www/
laravel new laravel
sudo chown $USER:www-data /var/www/laravel

Make project via Composer Create-Project

sudo mkdir /var/www/laravel
sudo chown $USER:www-data /var/www/laravel
cd /var/www/laravel

Install laravel via Composer create-project.

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

Then set access or permission to storage and bootstrap/cache directory.

sudo chown -R $USER:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache	

Test laravel in development.

php artisan serve

Then navigate to http://localhost:8000 or http://127.0.0.1:8000.

If you installed Laravel in public server or VPS and you need to access its public IP and add host and port option.

php artisan serve --host=202.47.46.35 --port=8080

Hit Ctrl+C to stop development server.

Step 8. Configure Virtual Hosts or Server Blocks

Following if you have domain name, you need to config virtual host or server block to your laravel’s root document.

After installing Laravel, you should configure your web server’s document / web root to be the public directory. The index.php in this directory serves as the front controller for all HTTP requests entering your application.

Apache

Create a virtual host file.

sudo nano /etc/apache2/sites-available/laravel.web.conf

Append with following.

<VirtualHost *:80>
    ServerName www.laravel.web
    ServerAlias laravel.web    
    DocumentRoot /var/www/laravel/public
    <Directory /var/www/laravel/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog /var/log/apache2/laravel.web.error.log
    CustomLog /var/log/apache2/laravel.web.access.log combined
</VirtualHost>		

Activate virtual host and restart service.

sudo a2enmod rewrite
sudo a2ensite laravel.web
sudo systemctl restart httpd

Navigate to http://www.laravel.web to test.

Nginx

Create a nginx server block under /etc/nginx/conf.d/

sudo nano /etc/nginx/conf.d/laravel.conf

Append below snippet.

server {
        listen 80;
        listen [::]:80;

        root /var/www/laravel;
        index index.php;

        server_name laravel.web;

        location / {
                try_files $uri $uri/ =404;
        }
}

Check if any errors occur.

sudo nginx -t

Restart service

sudo systemctl restart nginx

Step 9. Setting Database

If you want to access MySQL/MariaDB database you need to config following steps.

Edit .env file inside your root directory.

nano .env

Append this.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=password	

Next you need to make a register and login in Laravel.

Run migrate to make user database structure.

php artisan migrate

Add package ui.

composer require laravel/ui

Make authentication ui using Vue.js

php artisan ui vue --auth

Install npm.

yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_10.x | sudo -E bash -
sudo yum install nodejs

Then, run command below.

npm install
npm run dev

Navigate to your domain again. You will see Register and Login link already available. You can try it.

Laravel Register Login function
Laravel Register Login Function

Troubleshooting

There are maybe facing some trouble when installing Laravel. Following i already gather some trouble that i faced when installing Laravel. Comment below if you have any trouble.

# composer global require laravel/installer                                           Changed current directory to /root/.composer
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Using version ^2.2 for laravel/installer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for laravel/installer ^2.2 -> satisfiable by laravel/installer[v2.2.0].
    - laravel/installer v2.2.0 requires ext-zip * -> the requested PHP extension zip is missing from your system.

  To enable extensions, verify that they are enabled in your .ini files:
    - /etc/php.ini
    - /etc/php.d/10-opcache.ini
    - /etc/php.d/20-bcmath.ini
    - /etc/php.d/20-bz2.ini
    - /etc/php.d/20-calendar.ini
    - /etc/php.d/20-ctype.ini
    - /etc/php.d/20-curl.ini
    - /etc/php.d/20-dom.ini
    - /etc/php.d/20-exif.ini
    - /etc/php.d/20-fileinfo.ini
    - /etc/php.d/20-ftp.ini
    - /etc/php.d/20-gd.ini
    - /etc/php.d/20-gettext.ini
    - /etc/php.d/20-iconv.ini
    - /etc/php.d/20-intl.ini
    - /etc/php.d/20-json.ini
    - /etc/php.d/20-mbstring.ini
    - /etc/php.d/20-mysqlnd.ini
    - /etc/php.d/20-pdo.ini
    - /etc/php.d/20-pgsql.ini
    - /etc/php.d/20-phar.ini
    - /etc/php.d/20-posix.ini
    - /etc/php.d/20-shmop.ini
    - /etc/php.d/20-simplexml.ini
    - /etc/php.d/20-soap.ini
    - /etc/php.d/20-sockets.ini
    - /etc/php.d/20-sqlite3.ini
    - /etc/php.d/20-sysvmsg.ini
    - /etc/php.d/20-sysvsem.ini
    - /etc/php.d/20-sysvshm.ini
    - /etc/php.d/20-tidy.ini
    - /etc/php.d/20-tokenizer.ini
    - /etc/php.d/20-xml.ini
    - /etc/php.d/20-xmlwriter.ini
    - /etc/php.d/20-xsl.ini
    - /etc/php.d/30-mcrypt.ini
    - /etc/php.d/30-mysqli.ini
    - /etc/php.d/30-pdo_mysql.ini
    - /etc/php.d/30-pdo_pgsql.ini
    - /etc/php.d/30-pdo_sqlite.ini
    - /etc/php.d/30-wddx.ini
    - /etc/php.d/30-xmlreader.ini
    - /etc/php.d/30-xmlrpc.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

The error message complaining about the system cannot find php ext-zip. I tried to install php-zip but it said already installed.

# yum install php-zip
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Bad id for repo: CentOS Plus, byte =   6
Repository Extras is listed more than once in the configuration
Repository Fasttrack is listed more than once in the configuration
Repository OS is listed more than once in the configuration
Repository Update is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * epel: epel.dionipe.id
 * remi-php73: fr2.rpmfind.net
 * remi-safe: fr2.rpmfind.net
 * webtatic: uk.repo.webtatic.com
Package php73-common-7.3.10-1.el7.ius.x86_64 is obsoleted by php-common-7.3.11-1.el7.remi.x86_64 which is already installed
Nothing to do

To fix this problem, you need to install php-pecl-zip, then you can install laravel flawlessly.

# yum install php-pecl-zip
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Bad id for repo: CentOS Plus, byte =   6
Repository Extras is listed more than once in the configuration
Repository Fasttrack is listed more than once in the configuration
Repository OS is listed more than once in the configuration
Repository Update is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * epel: epel.dionipe.id
 * remi-php73: fr2.rpmfind.net
 * remi-safe: fr2.rpmfind.net
 * webtatic: uk.repo.webtatic.com
Resolving Dependencies
--> Running transaction check
---> Package php-pecl-zip.x86_64 0:1.15.5-1.el7.remi.7.3 will be installed
--> Processing Dependency: libzip5(x86-64) >= 1.5.2 for package: php-pecl-zip-1.15.5-1.el7.remi.7.3.x86_64
--> Processing Dependency: libzip.so.5()(64bit) for package: php-pecl-zip-1.15.5-1.el7.remi.7.3.x86_64
--> Running transaction check
---> Package libzip5.x86_64 0:1.5.2-1.el7.remi will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================
 Package                  Arch               Version                             Repository              Size
==============================================================================================================
Installing:
 php-pecl-zip             x86_64             1.15.5-1.el7.remi.7.3               remi-php73              51 k
Installing for dependencies:
 libzip5                  x86_64             1.5.2-1.el7.remi                    remi-safe               56 k

Transaction Summary
==============================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 107 k
Installed size: 281 k
Is this ok [y/d/N]: y
Downloading packages:
(1/2): php-pecl-zip-1.15.5-1.el7.remi.7.3.x86_64.rpm                                   |  51 kB  00:00:02
(2/2): libzip5-1.5.2-1.el7.remi.x86_64.rpm                                             |  56 kB  00:00:03
--------------------------------------------------------------------------------------------------------------
Total                                                                          34 kB/s | 107 kB  00:00:03
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libzip5-1.5.2-1.el7.remi.x86_64                                                            1/2
  Installing : php-pecl-zip-1.15.5-1.el7.remi.7.3.x86_64                                                  2/2
  Verifying  : php-pecl-zip-1.15.5-1.el7.remi.7.3.x86_64                                                  1/2
  Verifying  : libzip5-1.5.2-1.el7.remi.x86_64                                                            2/2

Installed:
  php-pecl-zip.x86_64 0:1.15.5-1.el7.remi.7.3

Dependency Installed:
  libzip5.x86_64 0:1.5.2-1.el7.remi

Complete!

Leave a Reply

Your email address will not be published. Required fields are marked *