Install Magento 2.4.7 on Ubuntu 22.04 [Complete Guide]

Install Magento 2.4.7 on Ubuntu 22.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.7, things have gotten more earnestly as Magento 2 currently.

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.

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

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

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


Configure Password Access for the MySQL Root Account

Login with root user.

sudo mysql

 

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

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;

exit

Create Magento 2 database

Lets create the database for our magento2: –

mysql -u magento2 -p

Then create

CREATE DATABASE magento2;
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 8.1 and packages with command:

sudo add-apt-repository ppa:ondrej/php && sudo apt update

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

Next, verify your PHP version:

php -v
Edit the /etc/apache2/mods-enabled/dir.conf file.
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.

sudo apt install php8.3-mbstring
sudo phpenmod mbstring

Enable the Apache rewrite module.

sudo a2enmod rewrite

Install PHP modules for Magento 2.4.7.

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

Reload Apache for the changes to take effect.

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:

sudo apt install openjdk-17-jdk

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

java -version

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

sudo apt install curl

Once done, run below command:-

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:
...
-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.8.1. Now we are ready to download Magento 2.4.7 using composer.

Step 6: Download and Install Magento 2.4.7

Download Magento2:-

Go to html folder by command:

cd /var/www/html

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

Command for this is :-

sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.7-p2 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.7

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.

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.7. 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

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

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

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

Modify file as shown below:

<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

If you are installing Magento locally, you can add domain.com. Then you have to update hosts file at /etc/hosts with

sudo nano /etc/hosts
127.0.0.1 sonal.magento.com
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).
Run the below command,
sudo a2ensite sonal.magento.com.conf
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

magento 2 successfully installed no sample data

 

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.4.4 website on localhost. Sample data will fill your website with some products, categories and images so your website will look like this:

magento 2.4 with sample data

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

magento 2.4 with sample data 2

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

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!