How to install Magento 2.4 on Ubuntu 20.04 or 22.04 and Linux using composer and config with Nginx server or Localhost (Full demo data). Previously in versions older than 2.4, it was possible to install Magento 2.x through a web browser but with version Magento 2.4.x, you can only install it using Command Line Utility (CLI).
Step-by-step: Install Magento 2.4 on Ubuntu 22.04 Using Composer
Step 1. Magento 2 installation Requirements
- Installed php7.4 successfully.
- Successfully installed MySQL 8
- Nginx has been installed successfully.
- Successfully installed Elasticsearch 7.x
Since Magento version 2.4 and above will use Elasticsearch instead of Mysql search, so you need to have a relative system configuration. (refer to system requirements )
Step 2. Install Magento 2.4 Ubuntu Using Composer
Since Magento became a part of Adobe, the old methods of install Magento 2.4 on Ubuntu or Debian by directly downloading the source code file are no longer in use. Therefore, you will need to download the source code to your computer through the composer.
- Reference link: Guide for install Composer on Ubuntu and Linux Debian
To be able to download the Magento 2 source, you also need to create authentication keys, log in to marketplace.magento.com, in the right corner of the screen click My Profile -> Access Keys
Click “Create A New Access Key“ to create your own Public Key, or Private Key, or get it here.
Public Key: e545ff23ec1afdd58dc63fe033da0305 Private Key: a8fc48a73b9b1ab6f92cf2e50418b6a1
Continue configuring Composer with authentication key:
composer global config http-basic.repo.magento.com <public-key> <private-key>
Next, select Magento version, there will be 2 versions of Open Source and Adobe Commerce, here ItsmeIT will install Magento 2.4 Ubuntu Open Source via composer with the following command:
sudo mkdir -p /var/www/vhosts/magento2.4 sudo chown -R itsmeit:www-data /var/www/vhosts/ cd /var/www/vhosts/magento2.4 composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.1
Enter the Public
, and Private Key
when asked, type the Y key, and Enter.
As you are using Nginx server, projects are usually located in the /var/www directory. In this step, ItsmeIT will create the /var/www/vhosts/magento2.4 directory and download the source code here. The path of this directory will be in the Nginx configuration file in step 6 below. Of course, you can change the directory path according to your project.
Note: Magento 2 is a very massive Framework, so please be patient and wait for the composer to download the source.
Step 3. Create a database and username in MYSQL
While waiting for a composer to download the source to install Magento 2.4 on Ubuntu 22.04 and 20.04 login to MYSQL and create a username, and database for the project.
sudo mysql
Or:
sudo mysql -u root -p
Then create a new database
CREATE DATABASE magento2 CHARACTER SET utf8 COLLATE utf8_general_ci;
Next, create a new MySQL user to manage the above database.
CREATE USER 'magento2'@'localhost' IDENTIFIED BY 'magento2@#$'; GRANT ALL PRIVILEGES ON magento2.* TO 'magento2'@'localhost'; FLUSH PRIVILEGES;
Step 4. Set permissions folders and files in Magento 2
Magento 2 is a tightly secured framework developed with a robust security system. Therefore, while installing and using it, you need to ensure that the folder permissions for the Magento files and source are correctly set. This step is crucial to prevent minor errors that can occur during the setup process when install Magento on Ubuntu.
sudo find /var/www/vhosts/magento2.4/* -type f -exec chmod 644 {} + sudo find /var/www/vhosts/magento2.4/* -type d -exec chmod 755 {} + sudo chown -R $USER:www-data /var/www/vhosts/magento2.4/*
Step 5. Install Magento 2.4 with CLI on Ubuntu 22.04
After completing the change permissions step, now you run the commands below one by one to install Magento 2.4 on ubuntu 20.04 or 22.04. Please check the user, database… in the command below to see if it is correct or not, and then start running the command on the terminal at the directory Magento.
cd /var/www/vhosts/magento2.4/project-community-edition
bin/magento setup:install \ --base-url=http://dev.magento2.com/ \ --db-host=localhost \ --db-name=magento2 \ --db-user=magento2 \ --db-password=magento2@#$ \ --admin-firstname=admin \ --admin-lastname=admin \ --admin-email=buivanloi.2010@gmail.com \ --admin-user=admin \ --admin-password=admin123 \ --language=en_US \ --currency=USD \ --timezone=Asia/Ho_Chi_Minh \ --use-rewrites=1 \ --search-engine=elasticsearch7 \ --elasticsearch-host=localhost \ --elasticsearch-port=9200 \ --elasticsearch-index-prefix=magento2 \ --elasticsearch-timeout=15
Explain:
- –base-url: On localhost, you can create any name as long as it is in the correct format, see the configuration in steps 6, 7.
- –db-host: the usual MySQL server is localhost.
- –db-name: the name of the database created in step 4.
- –backend-frontname: admin access key
- –db-user: username created in step 4.
- –db-password: password created in step 4.
- –admin-user: Username used to log in to the backend of the website.
- –admin-password: Password to login to the website backend.
- – Elasticsearch connects on port 9200
If there are no errors, the output will look like this:
Backend frontName (http://dev.magento2.com/admin_fry53j), you can also review and change in app/etc/env.php
Step 6. Setting Up Nginx Configuration File for Magento 2
To set up Magento 2.4 on Ubuntu 22.04, 20.04, or Linux with Nginx, you can create a configuration file named dev.magento.com.conf in the /etc/nginx/sites-available directory, where “dev.magento.com” is the domain name of your website. Then, add the following content to the file.
sudo nano /etc/nginx/sites-available/dev.magento.com.conf
upstream fastcgi_backend { server unix:/run/php/php7.4-fpm.sock; } server { listen 80; server_name dev.magento2.com www.dev.magento2.com; set $MAGE_ROOT /var/www/vhosts/magento2.4/project-community-edition; include /var/www/vhosts/magento2.4/project-community-edition/nginx.conf.sample; }
- upstream “unix”: Path to file php7.4-fpm
- server_name: your domain
- $MAGE_ROOT: Path to the directory containing the source Magento created in step 2, when you run composer it will automatically create a directory “project-community-edition”.
- include The exact path to the nginx.conf.sample file in the source directory Magento 2.
Press Ctrl + X select Y to save the file and run the command below to reset Nginx
sudo service nginx restart
Note: While install magento 2.4 on Ubuntu 22.04 and 20.04 with Nginx:
- If there is an error when working with Nginx, run the command:
sudo nginx -t
to display the error and check the configuration again. - To run the above config, you need to configure Nginx according to step 3 in this article.
Step 7. Configure Magento 2 Domain on Localhost
If you are install Magento 2.4 localhost Ubuntu, as you can see in the above steps, the domain “dev.magento2.com” is created during the installation process. Therefore, you need to configure this domain in the /etc/hosts file. Open this file and add the following content at the end to complete the install.
sudo nano /etc/hosts
Done, you can run the dev.magento2.com
website URL in your browser, and here are the results.
Step 8. Fix login backend error and switch Magento to dev mode.
Here is the solution to the issue of being unable to log in to the admin page after successfully install Magento 2.4 on Ubuntu 22.04, 20.04 or debian. By default, the Magento_TwoFactorAuth
module in Magento is enabled for two-factor security. You can temporarily disable this module to access the admin page. After that, you can re-enable it and configure it to enable the two-factor authentication if necessary. (reference)
To do this open terminal, cd to the directory Magento source and run the command disable the Magento_TwoFactorAuth
module, and switch to dev mode. Magento 2 has 3 modes: default, developer, and production, if you are building a website switch to the developer, and if the site is ready to go live, switch to production.
cd /var/www/vhosts/magento2.4/project-community-edition php bin/magento module:disable Magento_TwoFactorAuth php bin/magento deploy:mode:set developer php bin/magento cache:clean
You can also open the file /app/etc/config.php find the Magento_TwoFactorAuth module and change the value to 0 then run the cache:clean
command to clear the cache.
Step 9. Install Magento 2 sample data (Full demo)
To install Magento 2.4 on Ubuntu or Linux with FULL demo data, include pages, products, and categories, you can execute the command below from the Magento directory. Please note that the demo installation may take some time, so please be patient.
php bin/magento sampledata:deploy
Wait for sample data of magento 2.4 to be installed successfully, then run the upgrade command to update the source and database.
php bin/magento setup:upgrade
Step 10. How to delete/remove sample data Magento 2.4
You can also run the command below to remove demo data. (reference link)
php bin/magento sampledata:remove php bin/magento setup:upgrade
Above are the steps on how to install Magento 2.4 on Ubuntu 20.04 or 22.04 using composer and configure Magento 2 with Nginx. Hope it helps you.
Reference: https://devdocs.magento.com/guides/v2.4/install-gde/prereq/nginx.html
– Advertising –