Skip to main content

How to Install Magento 2 on Ubuntu: Complete Guide

Installing Magento 2 is the first step in creating a scalable, high-performing e-commerce business. However, to ensure a smooth process, it is critical to understand the system requirements and follow a structured Magento installation guide. This article, aimed at developers and IT companies, explains in detail how to configure the environment, optimize the server, and complete the installation via CLI.

1. Magento System Requirements: What You Need Before You Begin

Before installing Magento 2, verify that your server meets these minimum requirements:

Server Environment

  • Operating System: Linux (Ubuntu 20.04 or higher, Ubuntu 24.04 recommended).
  • Web server: Apache 2.4 or Nginx 1.18+.
  • Databases: MySQL 5.7+, MariaDB 10.4+, or PostgreSQL 9.6+ (for Magento Commerce).
  • RAM Memory: Minimum 2GB (recommended 4GB+ for production environments).

Software

  • PHP: Version 7.4, 8.0, or 8.1 (disable deprecated versions).
    • Mandatory extensions: curl, gd, intl, openssl, PDO, xsl, zip, mbstring, soap, iconv, bcmath.
    • Recommended extensions: redis, sodium, imagick for improved performance.
  • Composer 2.0+: Essential tool for managing dependencies.
  • SSLSL Certificate: Mandatory to protect data and users.

Recommended Magento Optimizations

  • Set memory_limit in php.ini to 2GB or higher.
  • Enable OPcache to speed up PHP execution.
  • Use Redis and Varnish for cache management.

Prepare an eCommerce for production? Contact now experienced Linux technician, he can help you get the best setup for Magento.

2. Magento 2 Installation Guide: Detailed Steps via CLI

Follow these steps to install Magento 2 using the command line (CLI), the most reliable method for professional environments.

Step 1: Prepare the Environment

If you don't already have NginX, MySQL, PHP or Elastic Search installed, follow these steps

  1. Upgrade your system:

    sudo apt update & sudo apt upgrade -y
  2. Install NginX

    sudo apt install nginx
  3. Install MySQL

    sudo apt install mysql-server
  4. Install PHP and necessary extensions (example for Ubuntu):

    sudo add-apt-repository ppa:ondrej/php
    sudo apt install \
    	php8.1 php8.1-{cli,mysql,curl,gd,intl,xsl,mbstring,zipper,soap,imagick,bcmath}
  5. Install Elasticsearch

    sudo wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
    sudo apt-get install apt-transport-https
    echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list
    sudo apt-get update && sudo apt-get install elasticsearch
    sudo systemctl daemon-reload
    sudo systemctl enable elasticsearch.service
    sudo systemctl restart elasticsearch.service

Step 2: Create Magento MySQL user

Create Magento user and database from MySQL shell

sudo mysql
CREATE USER 'magento2'@'localhost' IDENTIFIED BY 'your_secure_password';

CREATE DATABASE magento2;

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

FLUSH PRIVILEGES;
exit

Step 3: Download Magento 2 via Composer

  • Install composer

    sudo apt install curl php8.1-cli php8.1-mbstring git unzip
  • We install composer

    cd ~
    
    curl -sS https://getcomposer.org/installer | php
    
    sudo mv composer.phar /usr/local/bin/composer
  • Let's test if composer installation was successful

    composer -V
    Composer version 2.8.5 2025-01-21 15:23:40
    PHP version 8.3.17 (/usr/bin/php8.3)
    Run the "diagnose" command to get more detailed diagnostics output.
  • Create a Composer project in the /var/www/magento directory let's use the user root for simplicity, we'll change permissions later:

    sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.6 /var/www/magento
  • Insert the Magento Marketplace credentials (public/private keys) if required, they can be created on the Adobe portal.

    Warning from repo.magento.com: You haven't provided your Magento authentication keys. For instructions, visit https://devdocs.magento.com/guides/v2.3/install-gde/prereq/connect-auth.html
    Authentication required (repo.magento.com):
      Username:
      Password: 

Step 4: Configure Magento web server permissions

  • Assign folder ownership to the web user (the standard is ww-data):

    sudo chown -R www-data:www-data /var/ww/magento
    
    sudo find /var/www/magento -type d -exec chmod 755 {} \;
    
    sudo find /var/www/magento -type f -exec chmod 644 {} \;

Step 5: Run the Installation Script

Run the setup:install command with the required parameters:

cd /var/www/magento/
sudo -u www-data php bin/magento setup:install \
  --base-url=http://tuo-dominio.com \
  --db-host=localhost \
  --db-name=magento2 \
  --db-user=magento2 \
  --db-password='your_secure_password' \
  --admin-firstname=Admin \
  --admin-lastname=Magento \
  [email protected] \
  --admin-user=admin \
  --admin-password=AdminPassword123! \
  --language=en_IT \
  --currency=EUR \
  --timezone=Europe/Rome \
  --use-rewrites=1 \
  --search-engine=elasticsearch7 \
  --elasticsearch-host=localhost \
  --elasticsearch-port=9200
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_18h6jv

Explanation key parameters:

  • --base-url: The public URL of the site (uses HTTPS).
  • --search-engine: Configure Elasticsearch to improve search performance.
  • --use-rewrites=1: Enable SEO-optimized URLs.

Magento Admin URI

Printed URL from installation "Magento Admin URI", we will need it for installation

Step 6. Configure virtual host NginX for Magento 2

Edit the virtual host:

sudo nano /etc/nginx/sites-available/magento
upstream fastcgi_backend { unix server:/run/php/php8.1-fpm.sock; } server { listen 80; server_name your-domain.com; server_name www.tuo-dominio.com; set $MAGE_ROOT /var/www/magento; include /var/www/magento/nginx.conf.sample; }

Let's create a link in the site-enabled to enable the new vhost in NginX

sudo ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/

Restart the NginX service with:

sudo systemctl restart nginx.service

Step 7. Configure PHP-FPM

Increase RAM memory for PHP-FPM processes

sudo nano /etc/php/8.1/fpm/php.ini
memory_limit = 2G

Restart the php-fpm daemon:

sudo systemctl restart php8.1-fpm.service

3. Post-Installation: Verifications and Optimizations

Verification of Installation

  • Access the frontend at the URL http://tuo-dominio.com. You should see the default homepage.
  • Access the admin panel via the set URL http://tuo-dominio.com/"Magento Admin URI" with the set credentials.

Configuration Cron Job

Magento requires cron jobs for automatic operations (e.g. indexing):

php /var/www/magento/bin/magento cron:install

Enable Production Mode

Optimize performance and security with:

php bin/magento deploy:mode:set production

4. Troubleshooting Common Errors During Installation

Error: "PHP Extensions Missing"

Solution: Install missing extensions. Example for php-sodium:

sudo apt install php-sodium && sudo systemctl restart apache2 

Error: "Database Connection Failed"

  • Check that the MySQL user has the correct privileges:

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

Error: "Access Denied to Folder var/"

Solution:Reassign Permissions:

sudo chmod -R 755 /var/www/magento/var/ /var/www/magento/pub/

FAQs In-depth

1. Can I install Magento 2 without Composer?
Yes, but it is not recommended. Composer handles updates and dependencies efficiently.

2. How do I choose between Apache and Nginx?
Nginx offers better performance under load, but Apache is easier for beginners to configure.

3. Should I use Elasticsearch?
Mandatory for Magento 2.4+. It improves speed and accuracy of catalog searches.

4. How to backup database after installation?
Use mysqldump:

mysqldump -u magento2 -p magento_db > magento_backup.sql 

5. Why is Production mode important?
Disable debugging, minify CSS/JS and enable advanced optimizations.

Optimize your Magento Server

Rely on Linux specialists for tailored server configurations, monitoring and advanced optimizations. Maximum security and guaranteed performance. Contact us now 30 minutes of Free consultation!

.

Add new comment

Comment

  • Allowed HTML tags: <br> <p> <code class="language-*"> <pre>
  • Lines and paragraphs break automatically.
  • Only images hosted on this site may be used in <img> tags.