Ad

How to install Magento 2 on Centos 7 [Complete Guide]

Posted magento centos

Introduction

Magento is one of the most popular open-source e-commerce platform written in PHP powered by Zend framework. In this tutorial, you will learn the steps required to install and setup Magento on your Linux/Centos 7 server.

Prerequisites

  1. PHP 5.6.19 +
  2. MySQL 5.6 +
  3. Apache
  4. Recommended 2GB of RAM

Installation

Step 1: Install Apache

To install and start Apache use the following commands:

$ sudo yum install httpd
$ sudo systemctl start httpd
$ sudo systemctl enable httpd

Step 2: Create a new Apache virtual host and configure it.

Create a new configuration file name magento.conf:

$ sudo vim /etc/httpd/sites-available/magento.conf

Paste the following code into the newly created config file:

<VirtualHost *:80>
     ServerAdmin admin@domain.com
     DocumentRoot /var/www/html/magento/
     ServerName domain.com
     ServerAlias www.domain.com

     <Directory /var/www/html/magento/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

     ErrorLog /var/log/httpd/magento_error.log
     CustomLog /var/log/httpd/magento_access.log combined
</VirtualHost>

Now with this, you will be debugging all the Magento setup related errors in the magento_error.log and magento_access.log

Step 3: Installing PHP and dependencies

Use the below command to install all required PHP dependencies:

$ 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

Open the php.ini file:

$ sudo vim /etc/php.ini

Change the following credentials:

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 512M
upload_max_filesize = 128M
max_execution_time = 3600

Restart Apache service:

$ sudo systemctl restart httpd

Step 4:  Setting up MySQL

  • Download the rpm file
$ wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
  • Set the installation package
$ sudo rpm -ivh mysql57-community-release-el7-9.noarch.rpm
  • Install MySQL server
$ sudo yum install mysql-server
  • Start MySQL
$ sudo systemctl start mysqld
  • Set MySQL root password

- Once the installation completes, a temporary password will be generated in the mysqld.log file. You can access this password by the following command:

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

- Now you will get a message something like the following:

[Note] A temporary password is generated for root@localhost: ,b*-rkuIR4Zas

- Copy this password for the next step and enter the following command:

$ sudo mysql_secure_installation

- This will prompt you to enter the root password. You can enter the copied password which you got in the previous step.

- After entering the password you will be prompted to enter a new MySQL password.

- Enter a new 12 character password with uppercase, lowercase letters, numerical and a special character

- You will now be prompted with multiple questions on how to set up the MySQL installation. You can say yes to all except if asked to change the password again.

  • Login to MySQL
$ sudo mysql -u root -p
  • Create a Magento database and add User
mysql > create database magento;
mysql > CREATE USER 'magentouser'@'localhost' IDENTIFIED BY 'password';
mysql > GRANT ALL PRIVILEGES ON * . * TO 'magentouser'@'localhost';
mysql > FLUSH PRIVILEGES;
mysql > exit;

Step 5: Install composer

You need Composer to install all the Magento and its dependencies.

Use the below command to install composer globally on your ubuntu system:

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ sudo php composer-setup.php --install-dir=/usr/bin/ --filename=composer

Check the composer installation by the following command:

$ composer -v

Step 6: Acquire / Generate secret keys from your Magento market place user account. (Required only for installation through composer)

In order to install Magento in the next step, you will have to specify Public and Private keys from your Magento market-place account. If you haven't registered yet you can register here.

  • Login to Magento Marketplace here.
  • Click on your username dropdown in the top navbar > My Profile

Image

  • Click on [Access Keys]

Image

  • Click on [Create A New Access Key]

Image

  • Enter a key name and generate by clicking on ok

After this, your Public and Private key will be generated. Use these keys for installation in the next step.

Step 7: Install Magento

  • Installation using Composer

Enter your servers root directory

cd /var/www/html

Run the composer command to install Magento

sudo 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 your public key as the username and the private key as the password. It will take some time for the Composer to download and install all the required dependencies so be patient.

  • Installation using the zip file

Open this link.

In the download tab, select your preferred version and click on download.

Follow the below commands:

$ sudo mkdir /var/www/html/magento/
$ sudo unzip ~/Downloads/magento2-ce.zip -d /var/www/html/magento/

If you have downloaded zip file to your local machine follow these steps

# On local machine
$ scp -r magento2-ce.zip user@<your-domain-name/ip-address>:/var/www/html/magento/magento.zip
# On remote server
$ cd /var/www/html/magento/
$ sudo unzip magento.zip

Install dependencies from using composer

$ composer install

After following the installation steps we can now open the Magento setup in your specified domain name or server IP address. for example <www.domain.com> or IP address <67.52.33.248>

Common Errors and Exceptions

If you are getting 500 error after the installation please follow these steps:

  • This problem is probably due to directory access problems. To solve that use the following commands:
$ sudo chown -R www-data:www-data /var/www/html/magento
$ sudo chmod -R 755 /var/www/html/magento

You have to set the user group according to your setup.

Your user group will depend on your Http server instance. In our case it was www-data it might be nginx or apache by default depending on the setup. You can check that by using

$ ps aux | grep apache
$ groups <apache-user>

If you are still getting errors you can debug those errors by referring the following logs

tail -f /var/log/httpd/magento_error.log
tail -f /var/log/httpd/magento_access.log

Step 8: Magento Setup

  • After accessing your URL this screen will be displayed

Image

  • Click on Agree and Setup Magento
  • On the next screen click on Start Readiness Check

Image

  • Make sure that all the checks are positive and if there are any dependencies that are missing, it will be displayed with the red cross mark so you can install it and try again. After confirmation, go to the next step.

Image

  • Enter the database credentials for Magento you've had created earlier in this tutorial and click on next.

Image

  • In the next step, you can set the web configuration details as per your requirements.

Image

  • Next step you will configure your timezone, language, and currency settings. You can select or unselect the modules you want from the list in advanced configuration.

Image

  • In the next step, you will set your admin login credentials

Image

  • After clicking on the Next button, your Magento setup is ready to be installed so click on the Install Now button.

Now you just have to wait for the installation to complete.

After the successful installation, all of your Magento setup details will be shown, you can note these and click on Launch Magento Admin.

This will open the login panel for Magento and you can log in with your admin credentials.

Setting up Cron Jobs

Cronjob is nothing but a scheduler that runs a specified file path at a specified interval. Magento needs some of these Cron Jobs to be set up in order to function properly.

Use the below commands to add the cron:

$ cd /var/www/html/magento
$ sudo php bin/magento cron:install

You can check the installed Magento Cron Jobs by using the below command:

$ sudo crontab -l

You should find theses settings in your Cron:

* * * * * /usr/bin/php /var/www/html/magento/bin/magento cron:run 2>&1 | grep -v "Ran jobs by schedule" >> /var/www/html/magento/var/log/magento.cron.log

* * * * * /usr/bin/php /var/www/html/magento/update/cron.php >> /var/www/html/magento/var/log/update.cron.log

* * * * * /usr/bin/php /var/www/html/magento/bin/magento setup:cron:run >> /var/www/html/magento/var/log/setup.cron.log

And with this, your Magento setup is up and ready to be used for your E-Commerce site.

Conclusion

By following the above-mentioned steps you are now ready to create your e-commerce platform with a working Magento installation. In case you have encountered something, which is not covered as a part of this tutorial, then please feel free to share those below in comments.