Objective


Install Magento 2.2 on CentOS 7. The stack will require PHP 7.1, MariaDB 10.2, and Apache 2.4. To see the full system requirements to run Magento 2.2 click here.

  1. Install Apache 2.4
  2. Configure Apache
  3. Install PHP 7.1
  4. Configure PHP
  5. Install MariaDB 10.2
  6. Configure MariaDB
  7. Create Magento File System User
  8. Download & Extract Magento Files
  9. SELinux
  10. Set File Permissions

Install Apache 2.4

Install Apache on Centos 7

yum -y install httpd

Verify your install

httpd -v

Output Should Look Like This:

Start The Apache Server

sudo systemctl start httpd

Set The Apache Server To Startup On Boot

sudo systemctl enable httpd

Configure Apache

In this section we will configure Apache to host Magento from the directory /var/www/html

Create Your Virtual Host File

You will now create a config file with your preferred text editor.

 vi /etc/httpd/conf.d/vhost.conf´╗┐

The below example config file tells Apache what we are hosting and how to host it.

NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin webmaster@yourmagentostore.com
    ServerName yourmagentostore.com
    ServerAlias www.yourmagentostore.com
    DocumentRoot /var/www/html/
    ErrorLog /var/log/httpd/yourmagentostore.com-error.log
    CustomLog /var/log/httpd/yourmagentostore.com-access.log combined
</VirtualHost>

Restart Apache

sudo systemctl restart httpd

Test Apache

Go to the public ip of your server and see if you get the test page for Apache. It should look like this:

Setup Rewrite and .htacess For Apache

Now you will have to setup rewrites for apache or Magento will give you all kinds of trouble.

Using vi/vim or your favorite nix text editor go open:

vi /etc/httpd/conf/httpd.conf

Find the section that starts with:

/var/www/html

Change

AllowOverride None

To

AllowOverride All

Restart Apache

systemctl restart httpd

Install PHP 7.1

yum install -y http://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/ius-release-1.0-14.ius.centos7.noarch.rpm

yum -y update

Install PHP extensions:

yum -y install php71u php71u-pdo php71u-mysqlnd php71u-opcache php71u-xml php71u-mcrypt php71u-gd php71u-devel php71u-mysql php71u-intl php71u-mbstring php71u-bcmath php71u-json php71u-iconv php71u-soap

Restart Apache

sudo systemctl restart httpd

Verify that PHP 7.1 is installed:

php -v

Output should be similar to this:

PHP 7.1.25 (cli) (built: Dec 11 2018 09:32:31) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.1.25, Copyright (c) 1999-2018, by Zend Technologies

Configure PHP

To find the PHP config file (php.ini) run the following command:

php --ini

Look for the value of the Loaded Configuration File
Mine is: /etc/php.ini

php –ini output on CentOS 7

Open your php.ini file with your text editor:

vi /etc/php.ini

Set PHP Default Timezone

You will need to change the timezone value to your desired timezone. Use this link to find your value. I will be using the timezone America/Chicago.

Now find the line that starts with:

;date.timezone =

If the line uncommented it will start with a semi colon. Remove the semicolon and set your desire value like this:

date.timezone = America/Chicago

Set the Memory Limit

The memory limit might be set to something low like 128MB, Let’s change it to 2Gigs. Go find the line that starts with this:

memory_limit=128M

Change it to 2G

memory_limit=2G

Find OPcache configuration file. Typically on CentOS 7 systems, it can be found in: /etc/php.d/ My opcache configuration file was named: 10-opcache.ini.

vim /etc/php.d/10-opcache.ini

Find opache.save_comments and uncomment it. Make sure it’s set to 1.

opcache.save_comments=1

Restart Apache

systemctl restart httpd

Install MariaDB 10.2

Create a MariaDB 10.2 repository by creating this file:

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

Add this text to the file:

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Kick off the install:

yum install MariaDB-server MariaDB-client -y

Start the Mariadb Service:

systemctl start mariadb

Enable the service to start on boot.

systemctl enable mariadb

Make sure the service is running and the correct version is installed:

systemctl status mariadb 

Configure MariaDB

To configure Mariadb run this command.

mysql_secure_installation

Just press enter as their is no password for root.

Enter current password for root (enter for none): 

Next, you will setup a mariadb root password

Set root password? [Y/n] Y

Once you set the password you will do some basic house cleaning.

Remove anonymous uers? [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

Now setup the database instance for Magento. Login to Mariadb as root.

mysql -u root -p

Enter the following command to setup a database called magento with a user of magento with a password of p@ssw0rd2019###

create database magento;
create user magento IDENTIFIED BY 'p@ssw0rd2019###';
GRANT ALL ON magento.* TO magento@localhost IDENTIFIED BY 'p@ssw0rd2019###';
flush privileges;

Create Magento File System User

adduser magento

Set a password for the new user.

passwd magento

Set the user primary group to Apache

usermod -a -G apache magento

Test to see if the user belongs to the Apache group

groups magento

The results should look like this:

magento : apache

Restart Apache

systemctl restart httpd

Download & Extract Magento Files

You will need to login and download the Magento files from their web site. Then just SCP the files over to the web server.

scp Magento.tar.bz2 username@remote_host:/remote/directory/

Move the compressed file into the /var/www/html directory

mv Magento.tar.bz2 /var/www/html

Extract the files

tar xvjf Magento.tar.bz2

Configure SELinux

Set SELinux context on the below directories:

chcon -R --type httpd_sys_rw_content_t <your Magento install dir>/app/etc

chcon -R --type httpd_sys_rw_content_t <your Magento install dir>/var

chcon -R --type httpd_sys_rw_content_t <your Magento install dir>/pub/media

chcon -R --type httpd_sys_rw_content_t <your Magento install dir>/pub/static

chcon -R --type httpd_sys_rw_content_t <your Magento install dir>/generated

Enable inter-server communication

setsebool -P httpd_can_network_connect=1

File System Permissions

cd /var/www/html

Login as the file system user.

cd <your Magento install dir>
find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +

find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +

chown -R :apache .

chmod u+x bin/magento

Finished:

At this point the web installer should be ready:

http://<Magento host or IP>/<path to Magento root>/setup