1. Knowledge base
  2. Initial Setup
  3. How to Install Magento 2 on CentOS…

How to Install Magento 2 on CentOS 7

Updated on 25 September 2020 5 min read Snel.com support team

Introduction

Magento is the most popular open-source e-commerce store application. It is written in PHP and uses MySQL/MariaDB database server. It is fully customizable which gives you the ability to customize look, functionality and content of the store. It also comes with thousands of easy to install plugins and layouts. It is very secure and fully SEO ready. In this tutorial, we will install Magento 2.3 using Apache web server, PHP 7.2 and MariaDB 10.3.

Prerequisites

  • Cloud VPS or Dedicated Server with at least 4GB RAM and CentOS 7 installed.
  • You must be logged in via SSH as sudo or root user. This tutorial assumes that you are logged in as a sudo user.
  • A domain name pointed towards your VPS or Dedicated server. In this tutorial, we will use example.com. Replace all occurrences of example.com with your actual domain name.

Update the System

Update the system with the latest packages and security patches using these commands.

Code
sudo yum -y update

Install Apache Web Server

Run the following command to install Apache web server on your system.

Code
sudo yum -y install httpd

Start Apache web server and enable it to automatically start at boot time by running these commands.

Code
sudo systemctl start httpdsudo systemctl enable httpd

Allow port 80 to be accessed through the firewall by running.

Code
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent

Reload firewall so that new setting can take effect.

Code
sudo firewall-cmd --reload

Also, disable SELinux. Otherwise, we will get write permission errors during installations.

Code
sudo sed -i 's/enforcing/disabled/g' /etc/selinux/config /etc/selinux/configsudo setenforce 0

You can now visit example.com from your browser to see the default Apache web page.

Install PHP 7.2

Magento 2 is compatible with PHP version 7.1.x and PHP version 7.2.x. In this tutorial, we will install the PHP 7.2.x using IUS community repository.

Set up the IUS repository in your system by running.

Code
curl https://setup.ius.io | sudo bash

Install PHP 7.2 along with all the required PHP modules by running the command.

Code
sudo yum -y install php72u php72u-pdo php72u-opcache php72u-xml php72u-gd php72u-devel php72u-intl php72u-mbstring php72u-json php72u-iconv php72u-mysqlnd php72u-fpm php72u-bcmath php72u-soap unzip

PHP is now installed on your server.

Install MariaDB Server

Create the MariaDB 10.3 repository file into the system by running the command.

Code
curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash

Now install the MariaDB server.

Code
sudo yum -y install MariaDB-server MariaDB-client

Start the database server and enable it to automatically start at boot time.

Code
sudo systemctl start mariadbsudo systemctl enable mariadb

Set a root password and secure the MariaDB instance by running the command.

Code
sudo mysql_secure_installation

Create Database for Magento

Log in to your MariaDB instance as root user by running the command.

Code
mysql -u root -p

Run the following queries to create MySQL user and database for Magento. Make sure to change example values with actual ones. Make a note of the credentials as we will require it later during installation.

Code
CREATE DATABASE magento_data;GRANT ALL ON magento_data.* TO magento@localhost IDENTIFIED BY 'Strong-Password';FLUSH PRIVILEGES;

Install Composer

Composer is a dependency management tool for PHP projects. Download composer install script by running.

Code
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

Now, install composer using the following command.

Code
sudo php composer-setup.php --install-dir=/usr/bin/ --filename=composer

Obtain Free Access Key from Magento Site.

To install Magento using composer, we need the access keys from Magento. Sign up or Log in into Magento Marketplace and navigate to My Profile >> Access Keys in Marketplace tab.

magento-marketplace

Now, click on Create a New Access Key button to generate a new access key.

magento-access-key
Note the Access key for future references as it will also be required later in this tutorial.

Create a Magento Project using Composer

Switch to the web root directory of your server and create a Magento project by running the following command.

Code
cd /var/wwwsudo composer create-project --repository=https://repo.magento.com/ magento/project-community-edition magento

You will be prompted to provide a username password for this. Put the public key of the access key as the username and private key as the password. It will take some time for the Composer to download and install all the required dependencies.

Change the file permissions as required by Magento.

Code
cd /var/www/magentosudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +sudo chmod u+x bin/magento

Provide the ownership of the files to the Apache user.

Code
sudo chown -R apache:apache /var/www/magento

Create Apache Vhost for Magento

Create a new virtual host file for Magento site by running.

Code
sudo vi /etc/httpd/conf.d/magento.conf

Put the following configuration in the editor. Make sure to change the example.com with your actual domain.

Code
<VirtualHost *:80>        ServerName example.com        DocumentRoot /var/www/magento        ErrorLog /var/log/httpd/magento_error.log        CustomLog /var/log/httpd/magento_access.log combined         <Directory /var/www/magento >                Options FollowSymLinks                AllowOverride All        </Directory></VirtualHost>

Restart the Apache web server so that the changes we did can take effect.

Code
sudo systemctl restart httpd

Install Magento via Web Installer

Open http://example.com in your browser and you should see Magento asking you to accept the terms and conditions. You will be taken to a multi-step installer after you agree to the terms and conditions. Each step is explained below.

  • Readiness Check: This step tests if your server is correctly configured to install Magento. If you had followed the tutorial correctly, you should see all green in the readiness check.
  • Add a Database: Provide the database information which you created on step 5 of the tutorial.
  • Web configuration: Choose the store address and admin panel address. You can choose any random (non-guessable) address as admin panel so that your admin panel is secured from attacks.
  • Customize your Store: Choose default language, time zone and currency for your store.
  • Create Admin Account: Provide administrator information at this step.
  • Install: Click the install button to finally install Magento with your chosen configurations.

Once the installation is done. You can go back to http://example.com to see your storefront.

Conclusion

In this tutorial, we have installed a functional e-commerce store which is ready to be customized as per your need. Log in to the admin panel of your store to play around the settings to discover more. It is recommended to install an SSL on your website, follow this tutorial to learn more.

Was this article helpful?

Spot a mistake in this article? Email us at support@snel.com

This article was last checked on 25 September 2020. · 21,646 views

Your own VPS online within 2 minutes

Windows or Linux, root access and hosted in Rotterdam.

View VPS from € 6.99

Support

Can't figure it out? We are happy to help

You always talk to a real engineer. We typically respond within 15 to 30 minutes, and 24/7/365 for outages.

Call us

+31 88 3 088 099

Create a ticket

Through the client area we immediately see which service it is about.

Email us

Send your question via the contact form, also outside office hours.