Linux, Magento 2, PHP

Configure centos7 for Magento2

Magento is a free and open source content management system for e-commerce. It is written in PHP and uses MySQL to store its data. You can run any types of e-commerce website using Magento. If you are looking for a great e-commerce web application for online store, then Magento is the best option for you. Magento provides a basic theme that can be used to set up an e-commerce website easily. Magento allows its user to install themes and change the display of the website or its functionality.


  • A server running CentOS 7.
  • A root or non-root user with sudo privilege setup on your server.

Getting Started

Before starting, update your system with the latest available packages by running the following command:

sudo yum update -y

Once your system is up-to-date, you can proceed to the next step.

Install and Configure Apache

Install Apache 2.4:

sudo yum install httpd

Modify httpd.conf with your document root directory to point Apache to your site’s files. Add the <IfModule prefork.c> section below to adjust the resource use settings.

Before changing any configuration files, we recommend that you make a backup of the file. To make a backup:

cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup
File Excerpt: /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html/"
    StartServers        5
    MinSpareServers     20
    MaxSpareServers     40
    MaxRequestWorkers   256
    MaxConnectionsPerChild 5500

These settings can also be added to a separate file. The file must be located in the conf.module.d or conf directories, and must end in .conf, since this is the format of files included in the resulting configuration.

Configure Name-based Virtual Hosts

You can choose many ways to set up a virtual host. In this section recommend and explain one of the easier methods.

    1. Within the conf.d directory create vhost.conf to store your virtual host configurations. The example below is a template for website; change the necessary values for your domain:
      NameVirtualHost *:80
      <VirtualHost *:80>
          DocumentRoot /var/www/html/
          ErrorLog /var/www/html/
          CustomLog /var/www/html/ combined

      Additional domains can be added to the vhost.conf file as needed. To add domains, copy the VirtualHost block above and modify its values for each additional virtual host. When new requests come in from the internet, Apache checks which VirtualHost block matches the requested url, and serves the appropriate content:

ErrorLog and CustomLog entries are suggested for more specific logging, but are not required. If they are defined (as shown above), the logs directories must be created before you restart Apache.

Create the directories referenced above:

sudo mkdir -p /var/www/html/{public_html,logs}

Enable Apache to start at boot, and restart the service for the above changes to take effect:

sudo systemctl enable httpd.service
sudo systemctl restart httpd.service

You can now visit your domain to test the Apache server. A default Apache page will be visible if no index page is found in your Document Root as declared in /etc/httpd/conf/httpd.conf:

Configure firewalld to Allow Web Traffic

CentOS 7’s built-in firewall is set to block web traffic by default. Run the following commands to allow web traffic:

sudo firewall-cmd --add-service=http --permanent && sudo firewall-cmd --add-service=https --permanent
sudo systemctl restart firewalld

Install MySQL and Create a Database and User

As of this guide’s publication, Magento 2 is not compatible with MariaDB, which is normally an equivalent replacement for MySQL in CentOS 7. Instead, we’ll install MySQL 5.7 from a separate repository.

sudo wget -P /tmp/
sudo yum localinstall /tmp/mysql57-community-release-el7-7.noarch.rpm
sudo yum update
sudo yum install mysql-community-server
  • Start the MySQL daemon:
    sudo systemctl start mysqld

    When starting MySQL for the first time, a temporary password is generated for root access to the database. Use grep to find it in the log file:

    sudo grep 'temporary password' /var/log/mysqld.log

    If you previously installed MariaDB or MySQL and set a root password, this installation may not override your existing database credentials. If that’s the case, use the root password you set previously.

  • Secure your MySQL installation and change the root password with the mysql_secure_installation script:

    Log into the MySQL shell as the root user, entering your new password when prompted:

    mysql -u root -p

    Create a Magento database and user, and set the permissions. In this example, we’ll call our database and user magento, Replace [email protected] with a secure password. You may optionally replace the other values as well:

    CREATE DATABASE magento;
    CREATE USER 'magento' IDENTIFIED BY '[email protected]';
    GRANT ALL PRIVILEGES ON magento.* TO 'magento';

    This section assumes that your database is hosted on the same server as your Magento application. If this is not the case, perform these steps and then refer to Magento’s guide on using a remote database server.

    You may receive an error about the complexity of your password if it is not secure enough. By default, CentOS 7 enables the validate-password plugin for MySQL. For more information, refer to the official MySQL documentation.

    Don’t use an exclamation point (!) in your password. The Magento installation script does not parse them correctly in its command options.

    Exit the MySQL shell:


    Install and Configure PHP 7

    Magento is a PHP application, so you will need to install PHP 7 and make some adjustments to its settings.

    CentOS 7’s default repositories include PHP 5.4, which is not compatible with Magento 2. Install the IUS (Inline with Upstream Stable) repository to get PHP 7:

    sudo wget -P /tmp
    sudo yum localinstall /tmp/ius-release-1.0-14.ius.centos7.noarch.rpm
    sudo yum update

    Install PHP 7 and its required extensions from the IUS repository:

    sudo yum install php70u php70u-pdo php70u-mysqlnd php70u-opcache php70u-xml php70u-mcrypt php70u-gd php70u-devel php70u-mysql php70u-intl php70u-mbstring php70u-json php70u-iconv

    Modify the following settings in your php.ini file:

    max_input_time = 30
    memory_limit= 2G
    error_log = /var/log/php/error.log
    date.timezone = America/New_York

    This sets the time zone for PHP’s date() function and imposes a 2GB limit to the amount of memory PHP can use. This value is recommended for a 4GB Linode, but could be increased for a larger server.

    The value for date.timezone will vary based on your system’s time zone. Refer to the PHP time zone documentation and ensure this value matches the time zone you set when you configured your Linode.

    Create the log directory for PHP and give the Apache user ownership:

    sudo mkdir /var/log/php
    sudo chown apache /var/log/php

    Because you installed a PHP module for Apache in Step 2, restart the web server to apply the changes and allow Apache to serve PHP pages:

    sudo systemctl restart httpd

    Optional: You may want to take this opportunity to create a phpinfo.php page to ensure that PHP is active and working properly with Apache:

    <?php phpinfo(); ?>

    Once you’ve visited this page in your browser and confirmed that PHP is working, delete the test page.

Tags :