Installing Magento 2.4 on CentOs 7

Installing Magento 2.4 on CentOs 7
January 4, 2021
Installing Magento 2.4 on CentOs 7

      Magento is an Open Source software that allow you to build a unique online store from the ground up. If this is the first time you use magento, this guide will help you to install Magento on CentOs 7.

      Firstly, you must satisfy the following System requirements:

      - Operating systems: Centos 7

      - Memory requirement: at least 2GB of RAM.

      - Web server: Apache or Nginx ( in this guilde we uses Nginx)

      - Database : MySql 8.0

      - Elasticsearch.

      - PHP: Magento supports PHP 7.4.0. You can install Magento 2.4.0 with 7.3, but it is not tested or recommended. It is intended for upgrading from Magento 2.3.x to Magento 2.4.0.

      I. Install NginX:

      Run the following command to install Nginx:

      yum -y install epel-release

      yum -y install nginx

      After installation is complete, start nginx and configure:

      systemctl start nginx

      systemctl enable nginx

     II. Install and config PHP, PHP-FPM:

      a. Run the following command to install PHP and PHP-FPM:

     yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

     yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

     yum install yum-utils

     yum-config-manager --enable remi-php74

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

     yum -y install php-fpm

      b. Config PHP:

      - Open the /etc/php.ini file in an editor

      - Uncomment the cgi.fix_pathinfo line and change the value to 0.

      - Edit the file to match the following lines:       

memory_limit = 2G

max_execution_time = 1800

zlib.output_compression = On

      - Uncomment the session path directory and set the path:

session.save_path = "/var/lib/php/session"

      - Save and exit the editor.

      - Open /etc/php-fpm.d/www.conf in an editor.

      - Edit the file to match the following lines:

user = nginx

group = nginx

listen = /run/php-fpm/php-fpm.sock

listen.owner = nginx

listen.group = nginx

listen.mode = 0660

      - Uncomment the environment lines:

env[HOSTNAME] = $HOSTNAME

env[PATH] = /usr/local/bin:/usr/bin:/bin

env[TMP] = /tmp

env[TMPDIR] = /tmp

env[TEMP] = /tmp

      - Save and exit the editor.

      - Create a new directory for the PHP session path and change the owner to the nginx user and group:

mkdir-p /var/lib/php/session/

chown-R nginx:nginx /var/lib/php/

      - Create a new directory for the PHP session path and change the owner to the nginx user and group:

mkdir-p /run/php-fpm/

chown-R nginx:nginx /run/php-fpm/

      - Start the php-fpm service and configure it to start at boot time:

systemctl start php-fpm

systemctl enable php-fpm

      - Verify that the php-fpm service is running:

netstat -pl | grep php-fpm.sock

      III. Install and configure MySQL

      a. Installing MySQL 

       Run the following commands to install MySQL:

       wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

      sudo rpm -ivh mysql80-community-release-el7-3.noarch.rpm

      sudo yum install mysql-server

      b. Starting MySQL:

      sudo systemctl start mysqld

      sudo systemctl status mysqld

      During the installation process, a temporary password is generated for the MySQL root user. Locate it in the mysqld.log with this command:

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

      The password of root user will be shown like below screenshot:

      c. Configuring MySQL:

      Use this command to run the security script:

      sudo mysql_secure_installation

      This will prompt you for the default root password. As soon as you enter it, you will be required to change it.

      d. Create database for magento site:

      - Use this command to login:

      mysql -u root -p  

      Then enter the new password (you set in previous step).

       - Creating database , use this command:

Create database magento2_db; (change magento_db with the name you want).

      IV. Install ElasticSearch:

      Before installing elasticsearch, you have to install Java by following command:

sudo yum install java-1.8.0-openjdk.x86_64

      Now, download and install Elasticsearch:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-x86_64.rpm

sudo rpm -ivh elasticsearch-7.9.2-x86_64.rpm

      Enable and start Elaticsearch:

sudo systemctl enable elasticsearch.service

sudo service elasticsearch start

      V. Install Magento 2.4 via composer

      1. Install composer:

      Now magento does not support Composer 2.x so we have to install the lower version using below commands:

curl -O "https://getcomposer.org/download/1.10.17/composer.phar" 

chmod a+x composer.phar 

sudo mv composer.phar /usr/local/bin/composer

      2. Install Magento:

      - Change to the web server docroot directory or a directory that you have configured as a virtual host docroot. I suppose it is /var/www.

      cd /var/www/

      Magento Open Source

      composer create-project --repository=https://repo.magento.com/ magento/project-community-edition magento2

      Magento Commerce

      composer create-project --repository=https://repo.magento.com/ magento/project-enterprise-edition magento2

      When prompted, enter your Magento authentication keys. Your public key is your username; your private key is your password.

      Set read-write permissions for the web server group before you install the Magento software. This is necessary so that the and command line can write files to the Magento file system.

      cd /var/www/html/magento2

      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 {} +

      chmod u+x bin/magento

      - Install Magento from the command line. This example assumes that the Magento install directory is named magento2, the db-host is on the same machine (localhost), and that the db-name is magento2_db (named in previous step), db-user, and db-password are all magento ( you have to create mysql user "magento" and assign database magento2_db to it):

      php bin/magento setup:install \

      --base-url=http://mydomain.com \

      --db-host=localhost \

      --db-name=magento2_db \

      --db-user=magento \

      --db-password=magento \

      --backend-frontname=admin \

      --admin-firstname=admin \

      --admin-lastname=admin \

      --admin-email=admin@admin.com \

      --admin-user=admin \

      --admin-password=admin123 \

      --language=en_US \

      --currency=USD \

      --timezone=America/Chicago \

      --use-rewrites=1 \

      --search-engine=elasticsearch7 \

      --elasticsearch-host=localhost

      - Switch to developer mode:

       cd /var/www/html/magento2/bin

      ./magento deploy:mode:set developer  

      3. Configuring Nginx:

      - Create a new virtual host for your Magento site:

      vim /etc/nginx/conf.d/magento.conf

      - Add the following configuration:

upstream fastcgi_backend {

  server  unix:/run/php-fpm/php-fpm.sock;

}

server {

  listen 80;

  server_name www.mydomain.com;

  set $MAGE_ROOT /var/www/magento2;

  include /var/www/magento2/nginx.conf.sample;

}

      - Save and exit the editor.

      - Verify that the syntax is correct:

      nginx -t

      - Restart nginx:

      systemctl restart nginx

      4. Configuring SELinux and Firewalld

      SELinux is enabled by default on CentOS 7. Use the following command to see if it’s running:

sestatus

      To configure SELinux and firewalld:

      - Install SELinux management tools:

      yum -y install policycoreutils-python

      Run the following commands to change the security context for the Magento installation directory:

      semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/magento2/app/etc(/.*)?'

      semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/magento2/var(/.*)?'

      semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/magento2/pub/media(/.*)?'

      semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/magento2/pub/static(/.*)?'

      restorecon -Rv '/var/www/magento2/'

      Install the firewalld package:

      yum -y install firewalld

      Start the firewall service and configure it to start at boot time:

      systemctl start firewalld

      systemctl enable firewalld

      Run the following commands to open ports for HTTP and HTTPS so you can access the Magento base URL from a web browser:

      firewall-cmd --permanent --add-service=http

      firewall-cmd --permanent --add-service=https

      firewall-cmd --reload 

      Finally, restarting your server and open a browser, then access http://mydomain.com. You will see as below screenshot if the installation is successfully: