Aug 14, 2024 9 min read

How to Install Magento 2 on Ubuntu 22.04

Install Magento 2 on Ubuntu 22.04 with our step-by-step tutorial. It is an e-commerce platform, known for its versatility and user-friendliness.

Install Magento 2 on Ubuntu 22.04
Install Magento 2 on Ubuntu 22.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Magento 2 on Ubuntu 22.04, let’s briefly understand – What is Magento?

Magento 2 is a popular and powerful e-commerce platform, known for its versatility and user-friendliness. Released as an upgrade to the original Magento, it offers enhanced performance, flexibility, and a seamless shopping experience. With a wide range of customizable themes and extensions, Magento 2 empowers businesses of all sizes to create and manage online stores efficiently.

Its user-friendly interface, robust features, and SEO capabilities make it an ideal choice for businesses seeking to establish a strong online presence and boost sales.

This tutorial will help you through the steps involved in installing Magento 2 on Ubuntu 22.04. We will also address a few FAQs on how to install Magento 2 on Ubuntu 22.04.

Advantages of Magento 2

  1. Enhanced Performance: Magento 2 offers faster page loading times, reducing bounce rates and improving user experience.
  2. User-Friendly Interface: Its intuitive admin panel simplifies store management for non-technical users.
  3. Extensive Customization: A wide range of themes and extensions allow personalized online stores.
  4. Mobile-Optimized: Responsive design ensures seamless shopping across various devices.
  5. SEO Capabilities: Built-in SEO features boost search engine rankings, increasing online visibility and traffic.

Prerequisites to Install Magento 2 on Ubuntu 22.04

In order to make the installation a success, you're going to require the following:

  • A domain name pointing to your public server IP.
  • Apache is installed on your Ubuntu machine.
  • An SSL Certificate for your domain to encrypt user information.

Step 1: Install Apache 2.2, along with the required PHP Extensions

Apache server is the most common cross-platform HTTP server which is supported by Linux, Windows, and other operating systems.

1) In order to install Apache, you need to update the packages and then install Apache with the following command:

sudo apt update
sudo apt install apache2 -y

2) After installing Apache, you can check the Apache Version using the following command:

sudo apache2ctl -v

3) Use the following command if you want Apache to run automatically during startup:

sudo systemctl enable apache2.service

Step 2: Install MySQL and Create a database for Magento

Install MySQL by using the following command:

sudo apt install mysql-server

After installing MySQL, Configure Password Access for the MySQL Root Account.

sudo mysql
SELECT user,authentication_string,plugin,host FROM mysql.user;

NB: Replace the 'your_password' with your own secure password.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
exit

Then you need to create a user for Magento:

mysql -u root -p
SELECT user,authentication_string, plugin, host FROM mysql.user;
CREATE USER 'magento2'@'localhost' IDENTIFIED BY 'your_password';

After creating the user, you should modify the user and grant the permissions.

ALTER USER 'magento2'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'magento2'@'localhost' WITH GRANT OPTION;
exit

After that, create a database for Magento2.

mysql -u magento2 -p
CREATE DATABASE magento2;
exit

Step 3: Install PHP and required extensions

We want to install the PHP for Magento. Magento 2.4 versions need PHP 7.3 to 8.2. We need PHP 8.0 for Magento 2.4.5.

First, update the apt repositories.

sudo apt update

Then install PHP 8.1 and packages.

sudo apt install php8.1 libapache2-mod-php php-mysql

To verify the PHP version, you can use the following command:

php -v

Open /etc/apache2/mods-enabled/dir.conf file and edit the file.

sudo nano /etc/apache2/mods-enabled/dir.conf

Modify the index.php file order to the top listed in the DirectoryIndex.

After that, Install mbstring and enable the extension.

sudo apt install php8.1-mbstring
sudo phpenmod mbstring
sudo a2enmod rewrite

We want to install the PHP extensions for Magento 2.4.5

sudo apt install php8.1-bcmath php8.1-intl php8.1-soap php8.1-zip php8.1-gd php8.1-curl php8.1-cli php8.1-xml php8.1-xmlrpc php8.1-gmp php8.1-common

Then reload the Apache,

sudo systemctl reload apache2

After reloading the apache2, we need to make some changes in the configuration file. For that,

php -i | grep "Configuration File"

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

Search the values and update and save the file. Reload the Apache.

sudo systemctl reload apache2

Step 4: Install and configuration of Elasticsearch

Earlier, Elasticsearch is not mandatory in the installation of Magento. But from Magento 2.4, Elasticsearch is a mandatory component.

For the installation of Elasticsearch first, we can install Openjdk17 (Java).

sudo apt install openjdk-17-jdk

Using the command below, import the GPG key for the Elasticsearch packages:

sudo apt install curl
sudo curl -sSfL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --no-default-keyring --keyring=gnupg-ring:/etc/apt/trusted.gpg.d/magento.gpg --import

Then, add the Elasticsearch repository to the system:

sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
sudo chmod 666 /etc/apt/trusted.gpg.d/magento.gpg

The following commands must be used to refresh the cache after you have finished the above steps. And install Elasticsearch.

sudo apt update
sudo apt install elasticsearch

To start and enable the Elasticsearch engine, use the following commands below:

sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
sudo systemctl start elasticsearch.service

After installing Elasticsearch properly, you need to configure the Elasticsearch file. For that, open the elasticsearch.yml file and make some changes in the file.

sudo nano /etc/elasticsearch/elasticsearch.yml

Change the node.name and cluster.name variables.

node.name, as its name implies, defines the name of the server (node) and the cluster with which it is affiliated. If you don't modify these variables, a node.name will be assigned according to the server hostname automatically. The default cluster's name will be automatically configured as the cluster.name.

Elasticsearch's auto-discovery feature uses the cluster.name value to find and link Elasticsearch nodes to clusters automatically. As a result, if you leave the default value alone, your cluster may have unintended nodes that are connected to the same network.

Remove the # symbol to uncomment the line of node.name and cluster.name, network.host, http.port, and update the values.

...
node.name: "Your Node Name"
cluster.name: your application
network.host: 127.0.0.1
http.port: 9200
...

You need to modify this setting if your server only has 1GB of RAM.

sudo nano /etc/elasticsearch/jvm.options

Open the jvm.options file and update the Xms and Xmx values.

...
-Xms256m
-Xmx256m
...

After saving the file, start Elasticsearch,

sudo nano /usr/lib/systemd/system/elasticsearch.service

Set TimeoutStartSec as 900.

Then again reload the system,

sudo systemctl daemon-reload
sudo systemctl start elasticsearch.service

After the completion of the entire installation process of Elasticsearch, you need to test the Elasticsearch using curl. The elasticsearch uses 9200 port to run.

curl -X GET 'http://localhost:9200'

If the process is successful, you get the output as like following,

{
    "name": "Your Node Name",
    "cluster_name": "your application",
    "cluster_uuid": "ksht21377494",
    "version": {
        "number": "7.14.0",
        "build_flavor": "default",
        "build_type": "deb",
        .
        .
        .
        .
        .
    },
    "tagline": "Your search.."
}

If you get the output, elastic search installation process is completed. Then, we can install the composer.

Step 5: Install Composer

To install Composer using this command, start with the root directory,

cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/bin --filename=composer

To check the composer version use this command below,

composer

Step 6: Download and Install Magento2

Open the HTML folder in the terminal using the command,

cd /var/www/html

Create a composer project,

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

In the execution of this command, you should enter the username and password. To get the username and password, you should create an account in the Magento marketplace and get the username and password from https://marketplace.magento.com/customer/accessKeys/

How to Install Magento 2.4.5 in Ubuntu 22.04-cybrosys

Click Create A New Access Key to create the access keys. You will get a private key and a public key.

The public key will be the username and the private key will be the password.

You can enter the username and password in the terminal. After entering the username and password, downloading and installation will start.

After the completion of the process, set permissions:

cd /var/www/html/<magento install directory>
sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
sudo chown -R user_name:www-data .

Note: user_name is the user with root access

sudo chmod u+x bin/magento

All set, let's run the last command to complete the installation of Magento 2.4.

Initially, change the current directory to the folder for your website.

Open the install directory for Magento 2.

cd /var/www/html/<magento install directory>
sudo php bin/magento setup:install --base-url=http://<your-domain> --db-host=localhost --db-name=magento2 --db-user=magento2 --db-password=<your-db-password-of-magento2-user> --admin-firstname=Admin --admin-lastname=Admin [email protected] --admin-user=admin --admin-password=<your-admin-password> --language=en_US --currency=INR --timezone=Asia/Kolkata --backend-frontname=admin --search-engine=elasticsearch7 --elasticsearch-host=localhost --elasticsearch-port=9200

--base-URL: your domain, eg: http://cybrosys.magento.com.

--db-name: give your Magento database name that you created earlier

--db-user: give the database username that you created.

--db-password: password of the MySQL user

Hold off until the installation is complete. This will take some minutes to complete the installation.

When installing Magento, if you added a directory name to the hostname or IP address of your server to construct the base URL, you'll need to clear it out.

sudo nano /etc/apache2/sites-available/cybrosys.magento.com.conf

Edit the file as demonstrated below:

<VirtualHost *:80>
ServerAdmin master@localhost
DocumentRoot /var/www/html/magento2/pub
         ServerName cybrosys.magento.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/html">
AllowOverride all
</Directory>
</VirtualHost>

Restart the Apache,

sudo systemctl restart apache2

If you are installing Magento locally, then you have to update the host file at /etc/hosts.

127.0.0.1 cybrosys.magento.com

Save the file and run the below command:

sudo a2ensite cybrosys.magento.com.conf
php bin/magento indexer:reindex && php bin/magento se:up && php bin/magento se:s:d -f && php bin/magento c:f && php bin/magento module:disable Magento_TwoFactorAuth

Finally, you can browse the URL http://cybrosys.magento.com. Your page will be like this:

How to Install Magento 2.4.5 in Ubuntu 22.04-cybrosys

FAQs on How to Install Magento 2 on Ubuntu 22.04

Can I install Magento 2 on a Windows operating system?

Yes, Magento 2 can be installed on Windows, but it is recommended to use a Linux-based system like Ubuntu for better performance and compatibility.

Is it possible to install Magento 2 without using Composer?

No, Composer is the recommended method for installing Magento 2 due to its dependency management and ease of use.

Can I install Magento 2 on shared hosting?

Installing Magento 2 on shared hosting is possible, but it may have limitations depending on the hosting provider's environment and resources. Dedicated hosting or virtual private servers (VPS) are typically recommended for better performance.

Can Magento 1 extensions be used with Magento 2?

No, Magento 1 extensions are not compatible with Magento 2. However, there might be similar extensions available specifically designed for Magento 2.

Do I need to have prior knowledge of PHP programming to install Magento 2?

While prior knowledge of PHP programming can be beneficial, it is not essential for the installation process. Basic familiarity with Linux systems and command-line operations is sufficient.

Can I change the installation directory of Magento 2?

Yes, you can choose a different installation directory for Magento 2 during the setup process by specifying the desired path.

Can I install multiple instances of Magento 2 on one server?

Yes, it is possible to install multiple instances of Magento 2 on a single server. Each instance would require its separate configuration and database.

Conclusion

Magento 2 is now running from the default document root on your Ubuntu 22.04 server.

You can now configure DNS settings with your DNS provider to point your web server record to the IP address of your Ubuntu 22.04 server & your Magento 2 website will be displayed.

If you have any queries, feel free to ask them in the comment section, and we would be happy to help you resolve them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.