Magento

Install Magento 2 on Ubuntu/Debian

Install Magento 2 on Ubuntu/Debian machine

Introduction: Magento is a popular e-commerce platform that caters to a wide range of businesses. It is written in PHP and is known for its flexibility and scalability. One of the key advantages of Magento is that it is open-source, which means the source code is freely available for anyone to use and modify under the Open Software License.

When it comes to choosing the right version of Magento for your business, you have several options to consider. The platform offers three distinct editions to accommodate different budgets, functionality needs, and business requirements:

  • Magento Open Source: This edition is free to use and is an excellent choice for small to medium-sized businesses or startups with limited budgets. Adobe Commerce: Formerly known as Magento Commerce, this edition is a paid version of Magento. It is ideal for businesses looking for advanced features, customization options, and professional support.
  • Adobe Commerce Cloud: This edition is a cloud-hosted and fully managed solution, making it an attractive option for businesses that want to offload the technical aspects of hosting and server management.

We aim to install Magento on an Ubuntu server: 

Absolutely, installing Magento on your website involves a series of prerequisites and steps to ensure it works correctly. Here’s a summary of the essential things you need to know and install:

Please refer to the official Magento documentation for version-specific requirements.
Step1: Install Apache2, you can use the package manager for your specific operating system. Here are commands for some common Linux distributions:

For Ubuntu/Debian:

sudo apt update
sudo apt install apache2
sudo systemctl enable apache2.service
sudo service apache2 status

Step 2: Install MySQL and Create Database for Magento 2

Install MySQL (or MariaDB) using your package manager. For example, on Ubuntu/Debian:
sudo apt install mysql-serverall mysql-server

Secure your MySQL installation by running:

sudo mysql_secure_installation

Log in to MySQL and create a database and a user for Magento 2:

CREATE DATABASE magento;

CREATE USER ‘user’@’localhost’ IDENTIFIED BY ‘your_password’;

GRANT ALL PRIVILEGES ON magento.* TO ‘user’@’localhost’;

FLUSH PRIVILEGES;

Step 3: Install PHP and Required Extensions

Install PHP and the necessary extensions. Magento 2 has specific PHP requirements. Here’s how to install PHP on Ubuntu as an example:
Note: You can leverage multiple PHP versions and adjust them according to the specific requirements of different Magento versions

sudo apt install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-intl php-mcrypt php-xml php-xmlrpc php-soap php-bcmath php-json

You will get the path of ini file from the above command.

sudo nano <path_of_php.ini_file>

Open the file, and make some changes as follows.

max_execution_time=18000
max_input_time=1800
memory_limit=4G

Restart the server: sudo systemctl reload apache2

Step 4: Install and Configure Elasticsearch

Magento 2 requires Elasticsearch for advanced search capabilities. Install and configure Elasticsearch:

wget -qO – https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg –dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

echo “deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main” | sudo tee /etc/apt/sources.list.d/elastic-8.x.list

Update the apt package manager and install Elasticsearch:

sudo apt update && apt install elasticsearch

Then start and enable the service.

systemctl start elasticsearch
systemctl enable elasticsearch

Check status of elasticsearch  
systemctl status elasticsearch.service

To verify that Elasticsearch is running correctly, you will use the curl command:

curl -X GET “localhost:9200/”
{
  “name” : “abhinay-IdeaPad-5-15ITL05-Ua”,  “cluster_name” : “elasticsearch”,
  “cluster_uuid” : “HBGMaQkXSP2lw3zZNng_JQ”,
  “version” : {
“number” : “8.10.3”,
“build_flavor” : “default”,
“build_type” : “deb”,
“build_hash” : “c63272efed16b5a1c25f3ce500715b7fddf9a9fb”,
“build_date” : “2023-10-05T10:15:55.152563867Z”,
“build_snapshot” : false,
“lucene_version” : “9.7.0”,
“minimum_wire_compatibility_version” : “7.17.0”,
“minimum_index_compatibility_version” : “7.0.0”
  },
“tagline” : “You Know, for Search”
}

Step 5 :  Composer is a dependency management tool for PHP.
You can install it globally with these commands:

php -r “copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);”

php composer-setup.php –install-dir=/usr/local/bin –filename=composer

php -r “unlink(‘composer-setup.php’);

Step 6: Download and Install Magento 2

First, navigate to your web server’s document root. For example:

cd /var/www/html/r/www/html/

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

Install Magento : These commands should be executed in the Magento Root Directory at /var/www/html/magento2.

php bin/magento setup:install –base-url=http://www.devmagento.com/ –db-host=127.0.0.1 –db-name=magento –db-user=root –db-password=123 –admin-firstname=test –admin-lastname=test –admin-email=test@example.com –admin-user=admin –admin-password=admin123 –language=en_US –currency=USD –timezone=America/Chicago –use-rewrites=1

sudo php bin/magento setup:upgrade && sudo php bin/magento setup:di:compile && sudo php bin/magento setup:static-content:deploy -f

Once you’ve finished these steps, the next task is to configure your site within the Apache web server.

For creating file use: 

sudo touch /etc/apache2/sites-available/devmagento.conf

Now open the file :

sudo nano /etc/apache2/sites-available/devmagento.conf

<VirtualHost *:80>

    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log

    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

<VirtualHost *:80>

ServerName www.devmagento.com

DocumentRoot /var/www/html/magento/pub

</VirtualHost>

Now need to run few more commands.

sudo a2ensite devmagento.conf
sudo a2enmod rewrite
sudo service apache2 status

After that add your domain in host file.

Folder Path : /etc/host

127.0.0.1 www.devmagento.com

Open the link in  browser: http://devmagento.com/

Common Issues: 

  • Some time you faces permissions issues with linux mint or ubuntu.
  • Incase if you are getting issues then you can enable developer mode by command or from app/etc/env.php
    And set  ‘MAGE_MODE’ => developer,
    By command : php bin/magento deploy:mode:set developer

At times, your site may become inaccessible. In such instances, it’s advisable to inspect the devmagento.conf file for any modifications, and if necessary, rerun the commands.

  • sudo a2ensite devmagento.conf
  •  sudo a2enmod rewrite
  • sudo service apache2 status
Abhinay Singh

Abhinay Singh

About Author

Leave a comment

Your email address will not be published. Required fields are marked *