How to install magento 2 on linux server

How to Install Magento 2 on Linux Server

Magento 2 is a powerful and popular open-source e-commerce platform that allows you to create and manage online stores. Magento 2 offers many features and functionalities, such as product catalog management, shopping cart, checkout, payment, shipping, marketing, customer service, analytics and more. Magento 2 also supports multiple languages, currencies, stores and websites.

In this tutorial, we will show you how to install Magento 2 on a Linux server running Ubuntu 20.04 LTS. We will use Apache as the web server, MySQL as the database server and PHP as the scripting language. We will also install and configure some additional components and tools that are required or recommended for Magento 2, such as Composer, Elasticsearch and Redis.

Before you begin, make sure that you have a Linux server with root or sudo access and a domain name pointing to your server’s IP address. You can use any domain name of your choice, but for this tutorial we will use example.com as an example.

Step 1: Update the System and Install Apache

The first step is to update the system packages and install Apache web server. Apache is one of the most widely used web servers in the world and it can handle PHP scripts and serve static files.

To update the system packages, run the following commands:

sudo apt update
sudo apt upgrade

To install Apache web server, run the following command:

sudo apt install apache2

To check if Apache is installed and running, run the following command:

sudo systemctl status apache2

You should see an output similar to this:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-12-14 10:15:23 UTC; 5min ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 1010 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 1048 (apache2)
      Tasks: 55 (limit: 2286)
     Memory: 9.9M
     CGroup: /system.slice/apache2.service
             ├─1048 /usr/sbin/apache2 -k start
             ├─1049 /usr/sbin/apache2 -k start
             └─1050 /usr/sbin/apache2 -k start

Dec 14 10:15:23 ubuntu systemd[1]: Starting The Apache HTTP Server...
Dec 14 10:15:23 ubuntu apachectl[1010]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Dec 14 10:15:23 ubuntu systemd[1]: Started The Apache HTTP Server.

You can also verify that Apache is working by opening your web browser and typing your server’s IP address or domain name in the address bar. You should see a default Apache page like this:

Step 2: Install MySQL and Create a Database for Magento

The next step is to install MySQL database server and create a database for Magento. MySQL is a popular relational database management system that can store and manage data for web applications.

To install MySQL database server, run the following command:

sudo apt install mysql-server

To check if MySQL is installed and running, run the following command:

sudo systemctl status mysql

You should see an output similar to this:

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-12-14 10:18:36 UTC; 3min ago
   Main PID: 1327 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 2286)
     Memory: 331.4M
     CGroup: /system.slice/mysql.service
             └─1327 /usr/sbin/mysqld

Dec 14 10:18:34 ubuntu systemd[1]: Starting MySQL Community Server...
10:18:36 ubuntu systemd1: Started MySQL Community Server.

To secure MySQL installation and set a root password, run the following command:

sudo mysql_secure_installation

You will be asked a series of questions. You can answer them as follows:

  • Enter current password for root (enter for none): Press Enter
  • Set root password? [Y/n]: Y
  • New password: Enter a strong password
  • Re-enter new password: Repeat the 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

To create a database for Magento, run the following command:

sudo mysql -u root -p

Enter the root password that you have set earlier. You will see a MySQL prompt like this:

mysql>

At the MySQL prompt, run the following commands to create a database named magento and a user named magento with full privileges on the database. Replace magento_password with a strong password of your choice.

CREATE DATABASE magento;
CREATE USER 'magento'@'localhost' IDENTIFIED BY 'magento_password';
GRANT ALL ON magento.* TO 'magento'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 3: Install PHP and Extensions for Magento

The third step is to install PHP and the required extensions for Magento. PHP is a scripting language that can execute dynamic web pages and interact with databases. Magento 2 requires PHP 7.4 or later.

To install PHP and the required extensions for Magento, run the following command:

sudo apt install php7.4 libapache2-mod-php7.4 php7.4-common php7.4-bcmath php7.4-curl php7.4-dom php7.4-gd php7.4-intl php7.4-mbstring php7.4-mysql php7.4-soap php7.4-xsl php7.4-zip

To check if PHP is installed and running, run the following command:

php -v

You should see an output similar to this:

PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

To configure PHP settings for Magento, open the php.ini file with your preferred text editor:

sudo nano /etc/php/7.4/apache2/php.ini

Find and edit the following values as recommended by Magento:

  • memory_limit = 2G
  • max_execution_time = 1800
  • max_input_time = 1800
  • upload_max_filesize = 256M
  • post_max_size = 256M

Save and close the file.

To restart Apache and apply the changes, run the following command:

sudo systemctl restart apache2

Step 4: Install Composer for Magento

The fourth step is to install Composer for Magento. Composer is a dependency manager for PHP that can help you install and update Magento components and their dependencies. Magento 2 requires Composer 1.x or later.

To install Composer for Magento, run the following command:

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

This will download and install Composer as a file named composer.phar in your current directory.

To make Composer globally accessible, move it to a directory that is in your system PATH, such as /usr/local/bin. For example:

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

To check if Composer is installed and working, run the following command:

composer --version

You should see an output similar to this:

Composer version 2.1.14 2021-11-30 10:51:43

To use Composer to install and update Magento components, you will need to obtain authentication keys for the Magento code repository. You can get these keys from your Magento Marketplace account or create a new account if you don’t have one.

To get your authentication keys, follow these steps:

  • Log in to your Magento Marketplace account or create a new one.
  • Click on your profile icon in the top-right corner and select My Profile.
  • Click on Access Keys in the left sidebar.
  • Click on Create A New Access Key and enter a name for your key pair.
  • Click on Generate New to create your public and private keys.
  • Copy and save your keys somewhere secure. You will need them later when you use Composer.

You have successfully installed Composer and obtained your authentication keys for Magento.