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.
Install MySQL:
Install MySQL using apt command:
sudo apt install mysql-server
When prompted, type 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 the password.
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_password‘ with your password.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
Verify mysql.user table with the command:
SELECT user,authentication_string,plugin,host FROM mysql.user;
exit
Create a 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_password‘ with your password.
CREATE USER 'magento2'@'localhost' IDENTIFIED BY 'your_password';
ALTER USER 'magento2'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_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
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
Also, this line to install all the required php extensions required to run Magento 2.4
yum -y install php74u-pdo php74u-mysqlnd php74u-opcache php74u-xml php74u-gd php74u-devel php74u-mysql php74u-intl php74u-mbstring php74u-bcmath php74u-json php74u-iconv php74u-soap
Reload Apache for the changes to take effect.
sudo systemctl reload apache2
Install and configure Elasticsearch:
In this step, we will install Elasticsearch – a required component since Magento 2.4.
From Magento 2.4, catalog search will no longer use mysql, instead the system will use Elasticsearch by default.
First, we will install Openjdk11 (Java) as Elasticsearch runs on Java
sudo apt install openjdk-11-jdk
Verify Java version with the below command:
java -version
Now we can start installing 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
First, add port 8080 to apache listen port file located in: /etc/apache2/ports.conf
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
<VirtualHost *:8080>
ProxyPass "/" "http://localhost:9200/"
ProxyPassReverse "/" "http://localhost:9200/"
</VirtualHost>
Save the file and run this command to enable the newly created elasticsearch virtual host file
sudo a2ensite your-domain-elasticsearch
Restart apache to apply changes:
sudo service apache2 restart
Now start elasticsearch service to test if it’s working properly
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch
Run this line to verify if elasticsearch is working properly
curl -X GET 'http://localhost:9200'
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
Check Composer 2 is installed in Ubuntu?
composer
Download Magento 2.4.2
Go to html folder by command:
cd /var/www/html
Now we are ready to download Magento 2.4 using composer. Create an account on Magento marketplace and go to https://marketplace.magento.com/customer/accessKeys/ to get private and public access keys, which will be prompted in the next step
Run this command to download Magento 2.4 data
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=2.4.2-p1 magento2
Now enter Username and password to start downloading
Username: Your public Key
Password: Your private key.
You will see this screen if everything is fine. Now just wait some minutes, the composer is downloading Magento 2 to your server
Note: You may see this error when downloading Magento 2.4 via composer
apt-get install php7.4-{mysql,gd,curl,intl,bcmath,ctype,dom,iconv,mbstring,simplexml,soap,xsl,zip}
Restart apache2 to apply changes
service apache2 restart
Set folders & files permission:
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 {} +
sudo chown -R :www-data .
sudo chmod u+x bin/magento
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 \
--admin-email=admin@admin.com \
--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
Replace the following information with your information
--base-url : your domain, eg: mywebsite.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
Wait until the installation is successful.
Change DocumentRoot To Pub:
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
Grant Permission To Folders:
chmod -R 777 var
chmod -R 777 pub/static
chmod -R 777 generated
chmod -R 777 generated/
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
Congratulation, Magento 2.4 was successfully installed on your ubuntu server.
Now you can access your store at this url: http://mywebsite.com
Admin url: http://mywebsite.com/admin
Troubleshoot admin login authentication error:
Failed to send the message. Please contact the administrator
You need to configure Two-Factor Authorization in order to proceed to your store's admin area
An E-mail was sent to you with further instructions
This error happened because, since Magento 2.4, Two-Factor Authorization is enabled by default.
Disable Two-Factor Authorization
php bin/magento module:disable Magento_TwoFactorAuth
So we have successfully installed Magento 2.4.2 on Ubuntu.
This is the end of the How To Install Magento 2.4.2 On Ubuntu.