Install Magento 2 on Ubuntu 21.04 [Complete Guide]

Install Magento 2 on Ubuntu 21.04 [Complete Guide]

BEST HOSTING For MAGENTO2

Introducing Magento 2 is never a simple errand. Since the absolute first delivery, there are numerous issues clients may experience when introducing Magento 2.

With the most recent arrival of Magento 2.4.3, things have gotten more earnestly as Magento 2 currently, requires Elasticsearch as a compulsory part.

To make a Magento 2 establishment work appropriately in the Production climate, clients are prescribed to introduce Magento 2 in localhost first. At that point move the neighborhood establishment to a Magento 2 facilitating and update store’s Baseurl, and reindex Magento 2.

In this instructional exercise, we will experience the way toward introducing Magento 2 (with Elasticsearch) in Ubuntu.

If you are also want to install Magento2 on Windows. Click Here.

Install Magento 2.4.5 or Magento 2.4.6 on Ubuntu 22.04 [Complete Guide]

Contents-

  • Step 1: Install Apache2
  • Step 2: Install MySQL and Create Database for Magento2
  • Step 3: Install PHP and required extensions
  • Step 4: Install and configure Elasticsearch
  • Step 5: Install Composer
  • Step 6: Download and Install Magento2
  • Wrapping up

Before starting the installation, you can check theΒ system requirement for installing Magento2 here.

 

Step 1: Install Apache2

First, we will install Apache web server. Connect to your server using ssh protocol with root access and run this command:

Apache is available in Ubuntu’s default software repositories. For this reason, we’ll start by updating the local package index for the latest changes.

sudo apt update

sudo apt update

Then, lets install the apache2Β package.

sudo apt install apache2

 

The system will ask to continue yes/no. Input β€œy” then enter to proceed.

Once installed, you can check the installed Apache version with the following command:

sudo apache2ctl -v

 

Awesome, as you can see Apache 2.4 is compatible with Magento2.4.3 in the system requirement. We can proceed to the next step.

To verify if apache2 was install properly, in your browser, enter your domain or ip address or 127.0.0.1. If the result is apache default page => everything is fine

 

To enable auto startup for apache, use this command:

systemctl is-enabled apache2
Apache will be automatically started everytime you reboot your server.

Step 2: Install MySQL and Create Database for Magento2

Install MySQL using apt command:

sudo apt install mysql-server

 

When prompted, typingΒ YΒ to confirm installation and then pressΒ ENTER.

When the installation is complete, lets check the mysql version

 

MySQL 8 is compatible with Magento2.4.3 as mentioned in the system requirement. So lets continue..

When the installation is complete, run the following command to secure your MySQL. This command will remove some insecure default settings and block access to your database system.

sudo mysql_secure_installation

 

When prompted, pressΒ YΒ to setup validate password.

Next, select a level of password validation. Enter your root password. The server will show the password strength for the root password you just entered and the server will ask if you want to continue with that password. If you accept, pressΒ Y.

 

For the rest of the questions, pressΒ YΒ and pressΒ ENTERΒ key at each prompt.

When finished, Test login to MySQL with root user by the command:

sudo mysql

You should see output like this:

Then, exit MySQL console by command:

1
exit

Configure Password Access for the MySQL Root Account

Login with root user.

sudo mysql

 

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

 

Note: Replace β€˜your_secure_passwordβ€˜ with your password.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_secure_password';

 

Verify mysql.user table with the command:

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

 

exit

Create new MySQL user for Magento 2

Login with root user.

mysql -u root -p

 

SELECT user,authentication_string,plugin,host FROM mysql.user;
Note: Replace β€˜your_secure_passwordβ€˜ with your password.
CREATE USER 'magento2'@'localhost' IDENTIFIED BY 'your_secure_password';

 

ALTER USER 'magento2'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_secure_password';

 

Grant all privileges toΒ magento2Β users.

GRANT ALL PRIVILEGES ON *.* TO 'magento2'@'localhost' WITH GRANT OPTION;

 

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

 

exit

Create Magento 2 database

1
mysql -u magento2 -p

 

1
CREATE DATABASE magento2;

 

1
exit

 

Step 3: Install PHP and required extensions

In this step, we will install PHP.Β Magento 2.4Β require PHP 7.4, so we will install php 7.4 in this tutorial.

Update your APT repositories.

sudo apt update

Install PHP 7.4 and packages with command:

sudo apt install php7.4 libapache2-mod-php php-mysql

 

Next, verify your PHP version:

php -v

Edit the /etc/apache2/mods-enabled/dir.conf file.

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

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

The dir.conf file after modifying will look like this:

Install and enableΒ mbstringΒ extension.

1
sudo apt install php7.4-mbstring

1
sudo phpenmod mbstring

Enable the Apache rewrite module.

1
sudo a2enmod rewrite

Install PHP modules for Magento 2.4.x.

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

 

Reload Apache for the changes to take effect.

1
sudo systemctl reload apache2

Configure php settings

php -i | grep "Configuration File"

From this command, you will get path to the php.ini file. Open this file to modify some settings:

sudo nano <path_of_php.ini_file>

In php.ini file, search and change the following values as below

max_execution_time=18000
max_input_time=1800
memory_limit=4G

Save the file. Reload Apache2.

sudo systemctl reload apache2

These values will keep the installation go properly without interruption.

Step 4: Install and configure Elasticsearch

From Magento 2.4, Elasticsearch has become compulsory component. From Magento 2.4, catalog search will no longer use mysql, instead the system will use Elasticsearch by default.

So let’s install and configure it.

Install Elasticsearch

First, we will install Openjdk17 (Java) as Elasticsearch runs on Java:

1
sudo apt install openjdk-17-jdk

Next, verify if the java was installed properly and check its version with this syntax

1
java -version

The first step is toΒ import the GPG keyΒ for Elasticsearch packages using the following command:

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 you need toΒ add Elasticsearch repositoryΒ to the system using the following command:

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

After completing the above steps, you must firstΒ update the cacheΒ using the following commands. ThenΒ install Elasticsearch packagesΒ on the system:

sudo apt update
sudo apt install elasticsearch


Finally, you can use the following commandsΒ StartΒ andΒ enableΒ the Elasticsearch service:

sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service

sudo systemctl start elasticsearch.service

Configure Elasticsearch

Once the elasticsearch has been installed on your system, open the elasticsearch.yml configuration file.

Now that you have installed Elasticsearch and its Java dependency, it is time to configure Elasticsearch.

The Elasticsearch configuration files are in theΒ /etc/elasticsearch directory. The ones we’ll review and edit are:

  • elasticsearch.ymlΒ β€” Configures the Elasticsearch server settings. This is where most options are stored, which is why we are mostly interested in this file.
  • jvm.options β€” Provides configuration for the JVM such as memory settings.

The first variables to customize on any Elasticsearch server are node.nameΒ andΒ cluster.nameΒ inΒ elasticsearch.yml. Let’s do that now.

As their names suggest,Β node.nameΒ specifies the name of the server (node) and the cluster to which the latter is associated. If you don’t customize these variables, aΒ node.nameΒ will be assigned automatically in respect to the server hostname. TheΒ cluster.nameΒ will be automatically set to the name of the default cluster.

TheΒ cluster.nameΒ value is used by the auto-discovery feature of Elasticsearch to automatically discover and associate Elasticsearch nodes to a cluster. Thus, if you don’t change the default value, you might have unwanted nodes, found on the same network, in your cluster.

Let’s start editing the mainΒ elasticsearch.yml configuration file. Open it usingΒ nanoΒ or your preferred text editor:

sudo nano /etc/elasticsearch/elasticsearch.yml

Remove theΒ #Β character at the beginning of the lines forΒ node.nameΒ andΒ cluster.nameΒ to uncomment them, and then change their values. Your first configuration changes in theΒ /etc/elasticsearch/elasticsearch.ymlΒ file will look like this:

...

node.name: "My First Node"
cluster.name: my-application
...

Search for the line that contains network.host, uncomment it, and change the value to 0.0.0.0.

Set the network host to 0.0.0.0 to listen on all interfaces and make it available publicly,

network.host: 127.0.0.1
http.port: 9200

Incase you want to configure this to be private/local to your machine. You will have to set theΒ network.hostΒ toΒ 127.0.0.1, so the content is not public.

If your server only has 1GB of RAM, you must edit this setting.
Open jvm.options:
sudo nano /etc/elasticsearch/jvm.options
Now change theΒ XmsΒ andΒ Xmx values toΒ 256MB:
/etc/elasticsearch/jvm.options
...
-Xms256m
-Xmx256m
...
Save and exit the file. Now start Elasticsearch for the first time:
$ sudo nano /usr/lib/systemd/system/elasticsearch.service

TimeoutStartSec configures the time to wait for start-up. TIncrease this value to a proper value, in my case, I set it to 900.

sudo systemctl daemon-reload
sudo systemctl start elasticsearch.service
Allow some time for Elasticsearch to start before you attempt to use it. Otherwise, you may get a connection error.

Testing Elasticsearch

By now, Elasticsearch should be running on port 9200. You can test this using curl, the command-line tool for client-side URL transfers. To test the service, make aΒ GET request like this:

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

You will see the following response:

Output:

Now we have finished installing Elasticsearch.

Now let’s install next component – Composer.

Step 5: Install Composer

In this tutorial, we will install Magento 2.4 using composer. First, run this command to download composer installer.

Move back to the root directory.

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

Let’s check the composer version.

composer

Our composer version is 2.1.6.Β Now we are ready toΒ download Magento 2.4Β using composer.

Step 6: Download and Install Magento2

Download Magento2:-

Go toΒ htmlΒ folder by command:

1
cd /var/www/html

Create a new Composer project using the Magento Open Source or Magento Commerce metapackage.

Download Magento 2 with the specified version. we will use Magento Open Source and Magento version 2.4.3. So our command will be

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

Create an account on Magento marketplace and go toΒ https://marketplace.magento.com/customer/accessKeys/ to get private and public access key,

Create Access keys here. If you have access keys, you can use those.

Now enterΒ UsernameΒ Β andΒ passwordΒ to start downloading

  • Username: Your public KeyΒ  2fc966a913d4e83b28041eeb3c3b72e5
  • Password: Your private key. 48e05400d17ca1bcb4e693825c45416e

You will see this screen if everything is fine. Now just wait some minutes, composer is download Magento 2 to your server

Wait the process to complete.

Set file 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 .
sudo chmod u+x bin/magento
Note: sudo chown -R user_name:www-data .Β  => user_name is the user with root access

Install Magento 2

Everything is ready, now we will run the final command to install Magento 2.4

First, change the current directory to your website folderΒ (this is also where you downloaded Magento 2 data in step 3)

Go to Magento 2 install directory.

1
cd /var/www/html/<magento install directory>

You must use the command line to install Magento.

php bin/magento setup:installΒ --base-url=<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Β --admin-email=admin@admin.comΒ --admin-user=adminΒ --admin-password=<your-admin-password>Β --language=en_USΒ --currency=USDΒ --timezone=America/ChicagoΒ --backend-frontname=adminΒ --search-engine=elasticsearch7Β --elasticsearch-host=localhostΒ --elasticsearch-port=9200

Replace the following information with your information

--base-url : your domain, eg: sonal.magento.com. You can change this base URL later if you make mistake.

--db-hostΒ : Database host, inputΒ localhostΒ if you follow my tutorial

--db-nameΒ : name of the database we created in step 2

--db-userΒ : name of the database user we created in step 2

--db-passwordΒ : password of your mysql user

Now composer will start installing Magento 2.4. The process will take a while (approximately 20 minutes)

Wait until the installation is successful.

When everything is done you will see this

Congratulation, Magento 2.4 was successfully installed to your ubuntu server.

Change DocumentRoot To Pub

You can read detailed documentation from MagentoΒ here.

If you append a directory name to your server’s hostname or IP address to create the base URL when you installed Magento (for example http://<your-ip>/magento2 or http://<your-sever-hostname>/magento2), you’ll need to remove it.

Edit your virtual host file

1
sudo nano /etc/apache2/sites-available/000-default.conf

Add the path to your MagentoΒ pub/Β directory to theΒ DocumentRootΒ directive:

1
2
3
4
5
6
7
8
9
10
11
12
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/magento2/pub
Β  Β  Β  Β  Β ServerNameΒ sonal.magento.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/html">
AllowOverride all
</Directory>
</VirtualHost>

Restart Apache for the changes to take effect:

sudo systemctl restart apache2
Now,
sudo nano /etc/hosts
Add line at the last of this file
127.0.0.1 sonal.magento.com
Save the file (Ctrl + o and then Press Enter) and Close it (Ctrl + x).
Next, you will need to run these command to upgrade the database and deploy static view files
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

Hit the URL http://sonal.magento.com in the browser. This is Magento 2 Home page

 

Admin Login

Your Admin Url – http://sonal.magento.com/admin/Β 

When logging to admin dashboard withΒ username – adminΒ andΒ password – Admin@123456(which we set in magento install command),

 

Step 7: Install sample data for Magento 2

Now we will deploy sample data for our new Magento 2 website on localhost. Sample data will fill your website with some products, categories and images so your website will look like this:

 

To install the sample data, run the below command

php bin/magento sampledata:deploy && php bin/magento indexer:reindex && php bin/magento se:up && php bin/magento se:s:d -f && php bin/magento c:f

Well done! refresh your website and enjoy your new Magento 2.4 installation with sample data

 

You can watch complete video for this installation of Magento2 on Ubuntu.

Wrapping up

I have tried my best to give you the step-by-step tutorial so that it will be helpful for users who are having trouble installing Magento 2.4. If you face any problems during installation process, please drop a comment below (better with image describing your problem). I will be happy to help!

Recommended Posts

Sonal Motghare-Balpande
Sonal Motghare-Balpande

Hello Readers, With over 14 years of experience as a PHP expert and a Magento Certified Professional, I bring both technical mastery and a passion for growth. Beyond coding, I embrace a healthy lifestyle and believe in the transformative power of knowledge. My mission is to empower others through guidance because true success lies in sharing what we know. Let’s grow togetherβ€” https://www.facebook.com/SonalMotghareBalpande/ https://www.linkedin.com/in/sonalmotgharebalpande/ https://www.instagram.com/sonalmotgharebalpande/ Thank You, Sonal

One thought on “Install Magento 2 on Ubuntu 21.04 [Complete Guide]

  1. “The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later”.

    I’m getting the above error when trying to access the admin URL.

  2. I have installed the Magento 2 on ubuntu server. Do I have to install the XAMPP on windows machine to run the website?

  3. Hello Maam,
    previously i installed magento 2.4.3 on my windows by your tutorial it works fine , but now i just installed 2.4.5 on ubuntu live server , and add virtual host as you say , when i browse it show “We can’t connect to the server at desenfundanew.magento.com”

    can you please guide me ?

  4. Hello mam, you are doing such a great job! I have successfully installed magneto 2 with the help of composer and also all of its dependencies but when I curl it in my browser it says permission denied
    below is the success message of my work.
    ———————————————————————————————————-
    For security, remove write permissions from these directories: ‘/home/abdulqadir/magento/magento2/app/etc’
    [Progress: 1384 / 1385]
    Write installation date…
    [Progress: 1385 / 1385]
    [SUCCESS]: Magento installation complete.
    [SUCCESS]: Magento Admin URI: /admin
    Nothing to import.

    ————————–

    Composer version 2.4.0 2022-08-16 16:10:48

    ————————————

    virtual host file:

    VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/abdulqadir/magento/magento2/pub
    ServerName abdul.magento.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    AllowOverride all

    I am waiting for your reply thankyou

      1. Hello Maam ,
        Previously i installed 2.4.3 on my local server windows it run fine but now when i installed magento2.4.5 on ubuntu live server and add in virtual host as you say , and also change in core_config_data table for web/unsecure/base_url value as per vertual host server name , but in browser it show . ” We can’t connect to the server at desenfundanew.magento.com. why ?

  5. Sonal, your tutorials are absolutely detailed and we learn so much from you. I have an issue – magento2.4.4 setup on ubuntu 22.04 was successful. I am able to reach frontend and backend however when attempting to upload files magento2 crashes. please advise

  6. After I install.. the css and js files were not loading as it’s say not found ( 404) but when I search path in magento folder it’s their… but in browser it’s not loading… can u please help me here ??

      1. mam when i run this command he gave this error Error happened during deploy process: The path “/var/www/html/magento2/pub/static/deployed_version.txt” is not writable.

  7. Hello Sonal mam
    I follow all step. installation done. but front not show.
    root@jainam-Inspiron-3542:/var/www/html/magento2# tail /var/log/apache2/error.log
    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
    [Sat Jan 29 10:12:57.493273 2022] [mpm_prefork:notice] [pid 14307] AH00163: Apache/2.4.41 (Ubuntu) configured — resuming normal operations
    [Sat Jan 29 10:12:57.493293 2022] [core:notice] [pid 14307] AH00094: Command line: ‘/usr/sbin/apache2’
    [Sat Jan 29 10:16:59.239282 2022] [mpm_prefork:notice] [pid 14307] AH00171: Graceful restart requested, doing restart
    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
    [Sat Jan 29 10:16:59.301782 2022] [mpm_prefork:notice] [pid 14307] AH00163: Apache/2.4.41 (Ubuntu) configured — resuming normal operations
    [Sat Jan 29 10:16:59.301804 2022] [core:notice] [pid 14307] AH00094: Command line: ‘/usr/sbin/apache2’
    [Sat Jan 29 11:34:39.933857 2022] [mpm_prefork:notice] [pid 14307] AH00169: caught SIGTERM, shutting down
    [Sat Jan 29 11:34:40.090825 2022] [mpm_prefork:notice] [pid 23196] AH00163: Apache/2.4.41 (Ubuntu) configured — resuming normal operations
    [Sat Jan 29 11:34:40.090927 2022] [core:notice] [pid 23196] AH00094: Command line: ‘/usr/sbin/apache2’
    root@jainam-Inspiron-3542:/var/www/html/magento2#

  8. mam Please make a video on live server installation too .
    I did all steps but my website is showing the list of files instead or running magento website

Leave a Reply

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

πŸš€ Let’s Connect on LinkedIn! πŸš€

Want to level up your skills and knowledge? πŸš€
Follow us on LinkedIn to get:
βœ… Expert insights on business growth
βœ… Daily tips to sharpen your skills
βœ… Exclusive updates from The Coach SMB

Let's grow together!