How to Install Magento 2 on CentOS 8 Tutorial (Step by Step)

How to Install Magento 2 on CentOS 8. This article will  explain what Magento is and it will follow step by step guide how to install it. 

What is Magento?

Magento is a popular open source platform and is widely used in the E-commerce business because of its reliability. First released in 2008 and developed with PHP. eBay owns the Magento and two versions of Magento are available in the market: Magento open source and Magento commerce. Magento open-source is a free edition, whereas Magento commerce is a paid edition. Unlike other database platforms, Magento server uses a database model that is an “EAV”, the reason behind using this model is because of flexibility which is very important to access any E-commerce platform. The benefit of using the Magento platform is, it offers powerful marketing, better SEO (search engine optimization) and a catalog management tool for code reusing.

Magento 2

Magento 2 is an upgraded version of Magento that is faster (CMS Speed 50% faster), SEO friendly and more user friendly than Magento 1. Magento 2 supports the latest PHP and uses Secure Payment Gateways (Paypal, Braintree). Magento 2 also comes with the migration tool that easily migrates Magento 1 do Magento 2. I would like to add that Magento 2 has an improved performance (Varnish Caching) and have DevOps Tools for Marketing and advertising. 

Follow this article as we now show you how to install Magento 2 server on CentOS 8.

How to Install Magento 2 on CentOS 8

Install Apache, MariaDB and PHP

Magento uses MariaDB database and runs on a web server. So first install the Apache web server and MariaDB server package using the following command:

				
					dnf install httpd mariadb-server -y
				
			

After installing both packages, start Apache and MariaDB services and enable them to start at system reboot:

				
					systemctl start httpd mariadb 
systemctl enable httpd mariadb
				
			

Next, you will need to install PHP and other required extensions to your server. By default, the latest version of PHP is not included in the CentOS 8 default repository. So you will need to add EPEL and Remi PHP repository to your system. You can install both with the following command:

				
					dnf install epel-release -y
dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

				
			

Following step is to reset the default PHP repository and enable the PHP7.4 repository using the following command:

				
					dnf module reset php
dnf module install php:remi-7.4
				
			

What you need to do next is to install PHP with all required extensions using the following command:

				
					dnf install php php-cli php-soap php-pdo php-bcmath php-mysqlnd php-opcache php-xml php-gd php-intl php-mbstring php-json php-iconv php-zip unzip git -y

				
			

After the installation, open the php.ini file and change the default settings:

				
					nano /etc/php.ini
				
			

Change the following settings:

				
					memory_limit = 512M
upload_max_filesize = 64M
zlib.output_compression = On 
max_execution_time = 300 
date.timezone = UTC
				
			

Save and close the file when you are finished.

Create a Database for Magento

Firstly you will need to secure the MariaDB installation and set a MariaDB root password. You can do it with the following command:

				
					mysql_secure_installation
				
			

Answer all the questions as shown below:

				
					Enter current password for root (enter for none): 
Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Remove anonymous users? [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
				
			

Next you will log in to the MariaDB with the following command:

				
					mysql -u root -p
				
			

Once logged in create a database and user for Magento with the following command:

				
					MariaDB [(none)]> CREATE DATABASE magentodb;
MariaDB [(none)]> GRANT ALL ON magentodb.* TO magentouser@localhost IDENTIFIED BY 'password';
				
			

What you need to do next is to flush the privileges and exit from the MariaDB with the following command:

				
					MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;
				
			

Download Magento 2

Before downloading Magento 2, you will need to install Composer on your system. You can install it by running the following command:

				
					curl -sS https://getcomposer.org/installer | php
				
			

You should see the following output:

				
					All settings correct for using Composer
Downloading...

Composer (version 2.2.4) successfully installed to: /root/composer.phar
Use it: php composer.phar

				
			

Next, move the Composer binary to the system location with the following command:

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

Followint that you need to verify the Composer version:

				
					composer --version
				
			

Sample output:

				
					Composer version 2.2.4 2022-01-08 12:30:42
				
			

Please change the directory to Apache web root and download the latest version of Magento:

				
					cd /var/www/html/
wget https://github.com/magento/magento2/archive/refs/tags/2.4.2.zip
				
			

Once the Magento 2 is downloaded, unzip the downloaded file with the following command:

				
					unzip 2.4.2.zip
				
			

Then you must move the extracted directory to the Apache web root directory:

				
					mv magento2-2.4.2 /var/www/html/magento2
				
			

Following that step, change the directory to magento2 and update the composer with the following command:

				
					cd magento2
composer update
composer install
				
			

Next, set proper permission and ownership to magento2 directory:

				
					chown -R apache:apache /var/www/html/magento2
chmod -R 755 /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

				
			

Create an Apache Configuration File for Magento 2

In this section of how to Install Magento 2 on CentOS 8 you will need to create an Apache virtual host configuration file for Magento 2. You can create it with the following command:

				
					nano /etc/httpd/conf.d/magento.conf
				
			

Add the following lines:

				
					<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName magento2.example.com
DocumentRoot /var/www/html/magento2/
DirectoryIndex index.php

<Directory /var/www/html/magento2/>
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>

				
			

Save and close the file then restart the Apache service to apply the configuration changes:

				
					systemctl restart httpd
				
			

You can check the Apache status with the following command:

				
					systemctl status httpd
				
			

You should get the following output:

				
					● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           └─php-fpm.conf
   Active: active (running) since Tue 2022-01-18 11:04:03 EST; 6s ago
     Docs: man:httpd.service(8)
 Main PID: 6455 (httpd)
   Status: "Started, listening on: port 80"
    Tasks: 213 (limit: 25014)
   Memory: 28.9M
   CGroup: /system.slice/httpd.service
           ├─6455 /usr/sbin/httpd -DFOREGROUND
           ├─6457 /usr/sbin/httpd -DFOREGROUND
           ├─6458 /usr/sbin/httpd -DFOREGROUND
           ├─6459 /usr/sbin/httpd -DFOREGROUND
           └─6460 /usr/sbin/httpd -DFOREGROUND

Jan 18 11:04:03 centos8 systemd[1]: Stopped The Apache HTTP Server.
Jan 18 11:04:03 centos8 systemd[1]: Starting The Apache HTTP Server...
Jan 18 11:04:03 centos8 httpd[6455]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::200:6>
Jan 18 11:04:03 centos8 systemd[1]: Started The Apache HTTP Server.
Jan 18 11:04:03 centos8 httpd[6455]: Server configured, listening on: port 80

				
			

Install Magento 2 Using CLI

Next section you will need to perform the Magento 2 installation from the command-line interface because Magento web installation wizard has been removed since version 2.3.7.

First change the directory to Magento2 and disable the Elasticsearch module:

				
					cd /var/www/html/magento2/
sudo -u apache bin/magento module:disable {Magento_Elasticsearch,Magento_Elasticsearch6,Magento_Elasticsearch7}
				
			

Next, run the following command to start the installation:

				
					sudo -u apache bin/magento setup:install --admin-firstname="admin" --admin-lastname="admin" --admin-email="admin@example.com" --admin-user="admin" --admin-password="Secure@Password1234" --db-name="magentodb" --db-host="localhost" --db-user="magentouser" --db-password="password" --language=en_US --currency=USD --timezone=UTC --cleanup-database --base-url=http://"magento2.example.com"
				
			

After the successful installation, you should get the following output:

				
					[Progress: 826 / 831]
Installing admin user...
[Progress: 827 / 831]
Caches clearing:
Cache cleared successfully
[Progress: 828 / 831]
Disabling Maintenance Mode:
[Progress: 829 / 831]
Post installation file permissions check...
For security, remove write permissions from these directories: '/var/www/html/magento2/app/etc'
[Progress: 830 / 831]
Write installation date...
[Progress: 831 / 831]
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_dmxfpn
Nothing to import.

				
			

You will also need to install the Cron job for Magento 2. You can do it with the following command:

				
					bin/magento cron:install
				
			

Access the Magento 2 Web UI

Now, you can access the Magento 2 web UI using the URL http://magento2.example.com/admin_dmxfpn. You should see the Magento 2 login page:

Provide your admin username, password and click on the Login button. You should see the Magento dashboard in the following page:

How to Install Magento 2 on CentOS 8 Conclusion

In this guide, we explained what Magento is plus new improved Magento 2. We have moved onto  how to install Magento 2  on CentOS 8 server. You can now start your own online business using the Magento E-commerce platform.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x