How To Install Magento 2.4.2 On Ubuntu

Magento 2.4.2 has been released with a lot of security, GraphQL improvements concurrently with over 280 bug fixes. Please follow the post How To Install Magento 2.4.2 On Ubuntu to be able to install it successfully.

To start the installation, you should prepare yourself a VPS running Ubuntu operating system. In this post, we choose to use VPS provided by Vultr to bring speed and stability to Magento 2 website. If you have not used any VPS service, please click here to sign up for an account and get $100 for free.

1. Notes before installation

These are the changes of Magento 2.4.2 compared to previous versions. We need to know these before installation.

  • Magento 2.4.2 moved the index.php to the /pub/ folder for security purposes.
  • Supports Elasticsearch 7.9.x.
  • Compatible with Varnish 6.4.
  • Supports Redix 6.x.
  • Compatible with Composer 2.x.

2. Install and Configure Apache

You can refer to the post How To Install Apache On Ubuntu 20.04 to be able to install Apache 2.

3. Install MySQL

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, 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:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 807
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)
 
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> 

Then, exit MySQL console by command:

exit

You can install phpmyadmin to easily manage the database with this post How To Install PhpMyAdmin In Ubuntu.

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

mysql -u magento2 -p
CREATE DATABASE magento2;
exit

4. Install PHP 7.4

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

You should see output like 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

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:

<IfModule mod_dir.c>
        DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
 
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Install and enable mbstring extension.

sudo apt install php7.4-mbstring
sudo phpenmod mbstring

Enable the Apache rewrite module.

sudo a2enmod rewrite

Install PHP modules for Magento 2.4.2.

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.

sudo systemctl reload apache2

5. Install Elasticsearch

Install OpenJDK 11.

sudo apt install openjdk-11-jdk

Verify java version with below command:

java -version

Install Elasticsearch.

curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
sudo apt update
sudo apt-get install elasticsearch=7.9.3

Configure Elasticsearch

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart

Create new virtual host file for Elasticsearch proxy.

sudo nano /etc/apache2/ports.conf

Add Listen 8080 to the virtual host file. After modifying will look like this:

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
 
Listen 80
Listen 8080 //After Add Listen 8080
 
<IfModule ssl_module>
        Listen 443
</IfModule>
 
<IfModule mod_gnutls.c>
        Listen 443
</IfModule>
 
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
sudo nano /etc/apache2/sites-available/your-domain-elasticsearch.conf

Modifying file to below code:

<VirtualHost *:8080>
    ProxyPass "/" "http://localhost:9200/"
    ProxyPassReverse "/" "http://localhost:9200/"
</VirtualHost>

Now, Elasticsearch port is 8080. Keep this in mind, we will use it in the Magento 2.4.2 install statement.

sudo a2ensite your-domain-elasticsearch
sudo service apache2 restart

Start and enable Elasticsearch.

sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch

Verify Elasticsearch proxy with port 8080 by command:

curl -i http://localhost:8080/_cluster/health

6. Install Composer 2

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

You should see output like this:

All settings correct for using Composer
Downloading...
 
Composer (version 2.0.13) successfully installed to: /usr/bin/composer
Use it: php /usr/bin/composer

Check Composer 2 is installed in Ubuntu?

composer

If installed, you should see output like this:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.0.13 2021-04-27 13:11:08
 
Usage:
  command [options] [arguments]
 
Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
...

7. Download Magento 2.4.2

Go to html folder by command:

cd /var/www/html

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

Magento Open Source

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition <install-directory-name>

For example:

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

Download Magento 2 with the specified version.

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=<magento-version> <install-directory-name>

For example:

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

Magento Commerce

composer create-project --repository-url=https://repo.magento.com/ magento/project-enterprise-edition <install-directory-name>

Download Magento 2 with the specified version.

composer create-project --repository-url=https://repo.magento.com/ magento/project-enterprise-edition=<magento-version> <install-directory-name>

Set file permissions

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

8. Install Magento 2.4.2

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 \
[email protected] \
--admin-user=admin \
--admin-password=<your-admin-password> \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1 \
--search-engine=elasticsearch7 \
--elasticsearch-host=localhost \
--elasticsearch-port=8080

Wait until the installation is successful.

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

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

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

<VirtualHost *:80>
 
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/magento2/pub
 
        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

Update your base URL

Login with magento2 user.

mysql -u magento2 -p

Specify the Magento database you created when you installed Magento:

use magento2

Update the base URL:

For IP:

UPDATE core_config_data SET value='http://<your-ip>' WHERE path='web/unsecure/base_url';

For Server’s Hostname:

UPDATE core_config_data SET value='http://<your-server-hostname>' WHERE path='web/unsecure/base_url';

Update the env.php file

Go to env.php file.

sudo nano /var/www/html/magento2/app/etc/env.php

The node below needs to be in the file, check it out:

'directories' => [
    'document_root_is_pub' => true
]

Switch modes

Go to Magento 2 install directory.

cd /var/www/html/magento2

Switch to production mode.

php bin/magento deploy:mode:set production
php bin/magento cache:flush

Refresh your browser and verify that the storefront displays properly.

Switch to developer mode.

php bin/magento deploy:mode:set developer
php bin/magento cache:flush

Refresh your browser and verify that the storefront displays properly.

Verify the storefront

If you see the storefront showing a 500 internal server error, please fix set permissions for the Magento 2 file according to the post: How To Fix Magento 2 500 Internal Server Error.

10. Grant Permission To Folders

chmod -R 777 var
chmod -R 777 pub/static
chmod -R 777 generated
chmod -R 777 generated/

11. See Your Results

Don’t forget to run the following commands:

php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:f

If you have problems with the Admin Page: “Failed to send the message. Please contact the administrator”.

You need to disable the Two-Factor Authorization module using the following command:

php bin/magento module:disable Magento_TwoFactorAuth

So we have successfully installed Magento 2.4.2 on Ubuntu. Here are the results:

Magento 2.4.2 Storefront
Magento 2.4.2 Admin Panel

This is the end of the How To Install Magento 2.4.2 On Ubuntu.

Follow us for the more helpful posts!

We hope this is a useful post for you.

Thank you for reading!

4.7 30 votes
Article Rating

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
sajid831
sajid831
December 1, 2021 2:07 pm

Thank you very much!

This was a smooth installation article which help me to install Magento 2.4 without any issues.

Why O'Why
Why O'Why
December 9, 2021 3:43 pm

Why are adding a proxy with Apache instead of hiting ElasticSearch on :9200?

Eran Ariel
Eran Ariel
January 22, 2023 7:09 am

–base-url=http://localhost/magento2 can be configured during Instal which would already appear in core_config_data table, however making the change in the 000-default.conf, would it require to revise the configuratuin in the env.php with http://localhost/magento2?

I installed 2.45 on Ubuntu wsl, however received Index of /magento2 after install. Now I am receiving an error 500 after trying to create a domain name on localhost.

Please advise and thanks!

Aran

3
0
Would love your thoughts, please comment.x
()
x