How to Install Magento 2 on Ubuntu: Complete Guide
Installing Magento 2 is the first step in creating a scalable, high-performance e-commerce site. However, to ensure a smooth process, it is essential 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+.
- Database: MySQL 5.7+, MariaDB 10.4+, or PostgreSQL 9.6+ (for Magento Commerce).
- RAM: Minimum 2GB (4GB+ recommended for production environments).
Software
- PHP: Version 7.4, 8.0, or 8.1 (disable deprecated versions).
- Required extensions:
curl,gd,intl,openssl,PDO,xsl,zip,mbstring,soap,iconv,bcmath. - Recommended extensions:
redis,sodium,imagickfor improved performance.
- Required extensions:
- Composer 2.0+: Essential tool for managing dependencies.
- SSL certificate: Required to protect data and users.
Recommended Magento optimizations
- Set
memory_limittophp.inito 2GB or higher. - Enable OPcache to speed up PHP execution.
- Use Redis and Varnish for cache management.
Are you preparing an eCommerce site for production? Contact an experienced Linux technician now, who 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 interface (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
Update the system:
sudo apt update && sudo apt upgrade -y
Install NginX
sudo apt install nginxInstall MySQL
sudo apt install mysql-serverInstall PHP and necessary extensions (example for Ubuntu):
sudo add-apt-repository ppa:ondrej/phpsudo apt install \ php8.1 php8.1-{cli,mysql,curl,gd,intl,xsl,mbstring,zip,soap,imagick,bcmath}Install Elasticsearch
sudo wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpgsudo apt-get install apt-transport-httpsecho "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.listsudo apt-get update && sudo apt-get install elasticsearchsudo systemctl daemon-reload sudo systemctl enable elasticsearch.service sudo systemctl restart elasticsearch.service
Step 2: Create Magento MySQL user
Create the Magento user and database from the MySQL shell
sudo mysqlCREATE USER 'magento2'@'localhost' IDENTIFIED BY 'your_secure_password';
CREATE DATABASE magento2;
GRANT ALL PRIVILEGES ON magento2.* TO 'magento2'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;exitStep 3: Download Magento 2 via Composer
Install dependencies
composersudo apt install curl php8.1-cli php8.1-mbstring git unzipInstall
composercd ~ curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composerTest whether the Composer installation was successful
composer -VComposer 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 directory
/var/www/magentoUse the userrootFor simplicity, we will modify the permissions later:sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.6 /var/www/magentoEnter your Magento Marketplace credentials (public/private keys) if requested; these 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 ownership of the folder to the web user (the default is
www-data):sudo chown -R www-data:www-data /var/www/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 command setup:install 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=it_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_18h6jvExplanation of key parameters:
--base-url: The public URL of the site (use HTTPS).--search-engine: Configure Elasticsearch to improve search performance.--use-rewrites=1: Enable SEO-optimized URLs.
Magento Admin URI
Make a note of the URL printed by the "Magento Admin URI" installation, as we will need it for the installation.
Step 6. Configure NginX virtual host for Magento 2
Let's edit the virtual host:
sudo nano /etc/nginx/sites-available/magentoupstream fastcgi_backend {
server unix:/run/php/php8.1-fpm.sock;
}
server {
listen 80;
server_name tuo-dominio.com;
server_name www.tuo-dominio.com;
set $MAGE_ROOT /var/www/magento;
include /var/www/magento/nginx.conf.sample;
}Create a link in 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.serviceStep 7. Configure PHP-FPM
Increase the RAM memory for PHP-FPM processes
sudo nano /etc/php/8.1/fpm/php.inimemory_limit = 2GRestart the daemon php-fpm:
sudo systemctl restart php8.1-fpm.service3. Post-Installation: Checks and Optimizations
Verify the installation
- Access the frontend at the URL
http://tuo-dominio.com. You should see the default homepage. - Access the admin panel via the URL you set
http://tuo-dominio.com/"Magento Admin URI"with the credentials you set.
Cron Job Configuration
Magento requires cron jobs for critical automatic operations (e.g., indexing and sending emails). The automation and monitoring of these tasks are a fundamental aspect that we often take care of in our DevOps optimizations and consulting. An example of a cron:
php /var/www/magento/bin/magento cron:installEnable Production Mode
Optimize performance and security with:
php bin/magento deploy:mode:set production4. Resolving Common Installation Errors
Error: "PHP Extensions Missing"
Solution: Install the missing extensions. Example for php-sodium:
sudo apt install php-sodium && sudo systemctl restart apache2 Error: "Database Connection Failed"
Verify that the MySQL user has the correct privileges:
GRANT ALL PRIVILEGES ON magento2.* TO 'magento2'@'localhost'; FLUSH PRIVILEGES;
Error: "Access Denied to var/ Folder"
Solution: Reassign permissions:
sudo chmod -R 755 /var/www/magento/var/ /var/www/magento/pub/In-depth FAQ
1. Can I install Magento 2 without Composer?
Yes, but it is not recommended. Composer manages updates and dependencies efficiently.
2. How do I choose between Apache and Nginx?
Nginx offers significantly better performance under load, especially for demanding e-commerce sites such as Magento. If you need an advanced setup, check out our Nginx consulting and optimization services. Apache remains a viable alternative, easier to configure for beginners.
3. Do I have to use Elasticsearch? It
is mandatory for Magento 2.4+. It improves the speed and accuracy of catalog searches.
4. How do I back up the database after installation?
Use mysqldump:
mysqldump -u magento2 -p magento_db > magento_backup.sql 5. Why is Production mode important?
It disables debugging, minifies CSS/JS, and enables advanced optimizations.
Is Magento slow or unstable? Trust a Linux system administrator
A basic Magento 2 installation is not enough to handle the traffic peaks of a real e-commerce site. From Nginx and PHP-FPM tuning to advanced Redis and Varnish configuration, our Linux specialists create tailor-made server configurations to guarantee maximum performance and security.