How to Install Magento 2 on CentOS 7

Estimated reading time: 4 min


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.


  • 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 Replace all occurrences of with your actual domain name.

Step 1: Update the System

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

sudo yum -y update

Step 2: Install Apache Web Server

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

sudo yum -y install httpd

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

sudo systemctl start httpd
sudo systemctl enable httpd

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

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

Reload firewall so that new setting can take effect.

sudo firewall-cmd --reload

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

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

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

Step 3: 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.

curl | sudo bash

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

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.

Step 4: Install MariaDB Server

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

curl -sS | sudo bash

Now install the MariaDB server.

sudo yum -y install MariaDB-server MariaDB-client

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

sudo systemctl start mariadb
sudo systemctl enable mariadb

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

sudo mysql_secure_installation

Step 5: Create Database for Magento

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

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.

CREATE DATABASE magento_data;
GRANT ALL ON magento_data.* TO [email protected] IDENTIFIED BY 'Strong-Password';

Step 6: Install Composer

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

php -r "copy('', 'composer-setup.php');"

Now, install composer using the following command.

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

Step 7: 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.


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

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

Step 8: 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.

cd /var/www
sudo composer create-project --repository= 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.

cd /var/www/magento
sudo 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.

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

Step 9: Create Apache Vhost for Magento

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

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

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

<VirtualHost *:80>
        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

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

sudo systemctl restart httpd

Step 10: Install Magento via Web Installer

Open 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 to see your storefront.


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?
Dislike 2
Views: 14005

Reader Interactions


Leave a Reply

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