How to Install Magento 2 on CentOS 8
How to Install Magento 2 on CentOS 8

In this tutorial, we will show you how to install Magento 2 on a CentOS 8 VPS or dedicated server.

Magento is a free, open-source, and cloud-based e-commerce platform written in PHP. Magento offers a community and a commercial version of its platform – the community version is free and is designed primarily for individuals and/or small businesses. On the other hand, the enterprise version is mainly aimed at medium to large businesses and more of an enterprise environment.

Magento is very user-friendly and is fully customizable, allowing you to create and customize online stores in mere minutes. On top of the excellent customization, Magento offers powerful marketing, search engine optimization, and catalog-management tools. It allows you to run several stores at the same time and manage them using one panel. This is essentially the full package for e-commerce, providing everything you need to get started.

Let’s begin with the installation.

Prerequisites

  • A CentOS 8 VPS with root access enabled or a user with sudo privileges.
  • A valid domain name pointed with your server.
  • Minimum 4GB of RAM and 2 CPU cores.

Getting Started

First, you will need to log in to your CentOS 8 VPS via SSH as the root user:

ssh [email protected]_ADDRESS -p PORT_NUMBER

Make sure to replace “IP_ADDRESS” and “PORT_NUMBER” with your server’s IP (required) and Port number (if not the default).

Next, run the following commands to upgrade all installed packages on your VPS:

dnf update -y

By default, SELinux is enabled in CentOS 8 system. This can often cause conflicts and programs to stop working without any clear reason as to why, so it is recommended to disable SELinux on your system to ensure smooth functionality.

You can disable the SELinux by editing /etc/selinux/config file:

nano /etc/selinux/config

Find the following line:

SELINUX=enforcing

And, replace it with the following line:

SELINUX=permissive

Save and close the file. Then, restart your system to apply the changes.

Installing and Setting up Apache, MariaDB and PHP

In order for Magento to work, it requires a few prerequisite software packages. Apache provides the web server that serves the content, MariaDB is the database server that stores the data, and PHP is the programming language used to run the store.

First, install the Apache webserver and MariaDB with the following command:

dnf install httpd mariadb-server -y

The latest version of Magento is only compatible with PHP 7.1.3+ and 7.2.x. So you will need to install the supported PHP versions with required PHP extensions. By default, CentOS 8 ships with PHP version 7.2. This means that we won’t be needing a third-party repository in order to install PHP.  Therefore, you can simply install the required PHP version with all dependencies using the following command:

dnf install php php-cli php-mysqlnd php-opcache php-xml php-gd php-soap php-pdo php-bcmath php-intl php-mbstring php-json php-iconv php-zip unzip git -y

Once all the packages are installed, edit the php.ini file and tweak some settings:

nano /etc/php.ini

Change the following values:

memory_limit = 1024M
upload_max_filesize = 256M
zlib.output_compression = on
max_execution_time = 18000
date.timezone = UTC

Save and close the file then start the Apache and MariaDB service and enable them to start at boot with the following command:

systemctl start httpd
systemctl start mariadb
systemctl enable httpd
systemctl enable mariadb

At this point, the LAMP server is installed on your server.

Recommended Reading: How to Install WordPress with LEMP Stack on CentOS 8

Create a Database for Magento

First, secure the MariaDB installation and set the MariaDB root password with the following command:

mysql_secure_installation

A set of prompts will appear – this is how we answered all the questions, as shown below:

Enter current password for root (enter for none):
Set root password? [Y/n] Y
New password:
Re-enter new password:
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

Once the MariaDB is secured, log in to the MariaDB using the following command:

mysql -u root -p

Provide your MariaDB root password then create a database and user for Magento with the following command:

MariaDB [(none)]> CREATE DATABASE magento2;
MariaDB [(none)]> CREATE USER 'magento'@'localhost' IDENTIFIED BY 'password';

Make sure to REPLACE ‘password‘ with a SECURE password.

Next, grant all the privileges to the Magento database with the following command:

MariaDB [(none)]> GRANT ALL ON magento2.* TO 'magento'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

Next, flush the privileges and exit from the MariaDB with the following command:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

Install Magento

Before starting, it is recommended to create a non-root system user for Magento instance.

You can create a user called magento and then add them to the Apache webserver group with the following command:

adduser magento
usermod -a -G apache magento

Next, you will also need to install Composer on your system. Composer allows you to install all of the required PHP libraries and dependencies for your Magento project through one simple package manager.

You can install Composer with the following command:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Next, download the latest version of Magento from its official website.

Once the download is completed, unzip the downloaded file to the Apache web root directory with the following command. Note that the file name may be different for you, depending on the version that was downloaded:

unzip magento-ce-2.3.5-p1_sample_data-2020-04-24-10-19-21.zip -d /var/www/html/magento2

Next, change the directory to magento2 and install all required PHP dependencies with the following command:

cd /var/www/html/magento2
composer install

Next, set proper ownership and permissions to the magento directory with the following command:

chown -R magento:apache /var/www/html/magento2
chmod -R 775 /var/www/html/magento2

Configure Apache for Magento

Next, create an Apache virtual host configuration file for Magento with the following command:

nano /etc/httpd/conf.d/magento.conf

Add the following liens:

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName magento.example.com
DocumentRoot /var/www/html/magento2/
DirectoryIndex index.php
<Directory /var/www/html/magento2/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/httpd/magento_error.log
CustomLog /var/log/httpd/magento_access.log combined
</VirtualHost>

Save and close the file when you are finished. Then, restart the Apache service to implement the changes:

systemctl restart httpd

At this point, the Apache web server is configured to serve the Magento instance via HTTP.

Step 6 : Access Magento Web Interface

Now, open your web browser and type the URL http://magento.example.com. You will be redirected to the Magento welcome page:

Congratulations! That’s all there is to it – you have successfully installed and configured Magento 2 with Apache on CentOS 8.

LEAVE A REPLY

Please enter your comment!
Please enter your name here