Read in 6 minutes

last updated 

Install Magento 2 on CentOS 7

This is the first post in the series How to install and configure Magento 2 on CentOS 7. In this tutorial, we will walk you through the steps of installing Magento 2 on your CentOS 7 machine.

Magento is leading enterprise-class ecommerce platform built on open source technology combining powerful features with flexibility and user friendly interface.

With features like Engaging Shopping Experiences, Flexible Modular Architecture and Enterprise-grade Scalability and Performance Magento is a platform of choice for most online merchants.

Prerequisites

As the prerequisites to follow this tutorial, you will need:

  • CentOS 7 server, according to the official Magento 2 system requirements you need at least 2G of RAM. If you are using a server with less than 2GB of RAM, you should create a swap file.
  • A domain name pointing to your public server IP. In this tutorial we will use example.com.
  • A valid SSL certificate. If you don’t have one, you can create a free Lets’s Encrypt SSL certificate by following Secure Nginx with Let’s Encrypt on CentOS 7.
  • Installed and configured Nginx, MySQL and PHP 7.1. If you haven’t, refer to our LEMP on CentOS 7 guide.

Magento Access Key Pair

We need to generate access keys to authenticate access to the Magento 2 code repository and third-party extensions and themes.

If you don’t have a Magento Marketplace account, you can create one here. Once you create the account, please check these instructions on how to generate a new set of access keys.

Create MySQL Database

Magento 2 is compatible with MySQL 5.6 and 5.7, MariaDB 10.x and Percona 5.7. If you don’t have MySQL or MariaDB installed on your server you can check this guide.

Login to the MySQL shell:

mysql -u root -p

And run the following commands to create a new database and user:

create database magento;
GRANT ALL ON magento.* TO magento@localhost IDENTIFIED BY 'P4ssvv0rD';

Install PHP extensions

We assume that you have already enabled PHP 7.1 Remi repository using our guide.

Install all required PHP extensions:

sudo yum install php-mysql php-opcache php-xml php-mcrypt php-gd php-soap php-redis \
                 php-bcmath php-intl php-mbstring php-json php-iconv php-fpm php-zip

And set the required and recommended PHP options:

sudo sed -i "s/memory_limit = .*/memory_limit = 756M/" /etc/php.ini
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 256M/" /etc/php.ini
sudo sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php.ini
sudo sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php.ini
sudo sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php.ini
sudo sed -i "s/;opcache.save_comments.*/opcache.save_comments = 1/" /etc/php.d/10-opcache.ini

Install Composer

Composer is a dependency manager for PHP which is used for installing, updating and managing libraries. To install it globally run:

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

Create new system user

Create a new user and group, which will run our Magento installation, for simplicity we will name our user magento:

sudo useradd -m -U -r -d /opt/magento magento

Add the nginx user to the new user group and change the /opt/magento directory permissions so that the Nginx can access our Magento installation:

sudo usermod -a -G magento nginx
sudo chmod 750 /opt/magento

Configure PHP FPM

Next we need to configure PHP and create a FPM pool for our magento user.

/etc/php-fpm.d/magento.conf
[magento]
user = magento
group = nginx
listen.owner = magento
listen.group = nginx
listen = /run/php-fpm/magento.sock
pm = ondemand
pm.max_children =  50
pm.process_idle_timeout = 10s
pm.max_requests = 500
chdir = /

Restart the PHP FPM service for changes to take effect:

sudo systemctl restart php-fpm

Install Magento

There are several ways to install Magento. Avoid installing Magento from the Github repository because that version is intended for development and not for production installations. In this tutorial we will install our software from the Magento repositories using composer.

Switch over to the magento user:

sudo su - magento

and start the installation by downloading magento files to the /opt/magento/public_html directory:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition /opt/magento/public_html

During the project creation, the composer will ask you to enter the access keys, copy the keys from your Magento marketplace account and store them in the auth.json file, so later when updating your installation you don’t have to add the same keys again.

    Authentication required (repo.magento.com):
      Username: e758ec1745d190520ca246e4e832e12c
      Password:
Do you want to store credentials for repo.magento.com in /opt/magento/.config/composer/auth.json ? [Yn]

Once our project is created we can start the installation. We can install Magento either by using the command line or using the web Setup Wizard. In this tutorial we will install Magento using the command line.

We will install our Magento store with the following options:

  • Base and Base secure URLs are set to https://example.com, change it with your domain.
  • Magento administrator:
    • John Doe as first and last name.
    • [email protected] as email.
    • john as username and j0hnP4ssvv0rD as password.
  • Database name magento, username magento, password P4ssvv0rD and the database server is on the same host as the web server.
  • en_US, US English as default language.
  • USD dollars as default currency.
  • America/Chicago as time zone.

You can find all the installation options here.

Switch to the Magento directory:

cd ~/public_html

Run the following command to start the installation:

php bin/magento setup:install --base-url=https://example.com/ \
                        --base-url-secure=https://example.com/ \
                        --admin-firstname="John" \
                        --admin-lastname="Doe" \
                        --admin-email="[email protected]" \
                        --admin-user="john" \
                        --admin-password="j0hnP4ssvv0rD" \
                        --db-name="magento" \
                        --db-host="localhost" \
                        --db-user="magento" \
                        --currency=USD \
                        --timezone=America/Chicago \
                        --use-rewrites=1 \
                        --db-password="P4ssvv0rD"

Don’t forget to change the password (j0hnP4ssvv0rD) to something more secure.

If the installation is successful you will be presented with a message that contains the URI to the Magento admin dashboard.

[Progress: 485 / 485]
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_1csalp
Nothing to import.
Advertisement

Create the Magento crontab

Magento uses cron jobs to schedule tasks like re-indexing, notifications, sitemaps, emails and more.

To create the Magento crontab run the following command as magento user:

php ~/public_html/bin/magento cron:install

We can verify that the crontab is installed by running:

crontab -l
#~ MAGENTO START adc062915d7b30804a2b340095af072d
* * * * * /usr/bin/php /opt/magento/public_html/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /opt/magento/public_html/var/log/magento.cron.log
* * * * * /usr/bin/php /opt/magento/public_html/update/cron.php >> /opt/magento/public_html/var/log/update.cron.log
* * * * * /usr/bin/php /opt/magento/public_html/bin/magento setup:cron:run >> /opt/magento/public_html/var/log/setup.cron.log
#~ MAGENTO END adc062915d7b30804a2b340095af072d

Configure Nginx

If you followed our LEMP on CentOS 7 guide you should already have Nginx installed on your machine. Now we only need to create a new server block for our Magento installation. We are going to include the default nginx configuration shipped with magento:

/etc/nginx/conf.d/example.com.conf
upstream fastcgi_backend {
  server   unix:/run/php-fpm/magento.sock;
}

server {
    listen 80;
    server_name example.com www.example.com;

    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;

    set $MAGE_ROOT /opt/magento/public_html;
    set $MAGE_MODE developer; # or production

    access_log /var/log/nginx/example.com-access.log;
    error_log /var/log/nginx/example.com-error.log;

    include /opt/magento/public_html/nginx.conf.sample;
}

Reload the Nginx service for changes to take effect:

sudo systemctl reload nginx

Finally you should be able to login to your Magento installation at https://example.com/admin_1csalp using the admin user you specified when you run the Magento installer.

Conclusion

In this tutorial, you installed Magento 2 on your Centos 7 server. You have also generated a free Let’s encrypt SSL certificate and set up Nginx as a SSL termination proxy.

In the next part of this series, we will show you how to Install and configure Varsnish installation and configuration. Stay tuned!

This post is a part of the How to install and configure Magento 2 on CentOS 7 series.
Other posts in this series:

Install Magento 2 on CentOS 7