Magento is an enterprise-class, open-source e-commerce platform written in PHP. It is built for merchants who need a flexible, scalable store that can handle large product catalogs, complex pricing rules, and high traffic volumes. Its modular architecture means you can extend or customize almost any part of the platform.
This guide shows you how to install Magento 2 on Ubuntu 18.04 using Composer, Nginx, PHP 7.2, and MySQL. Before starting, create a Magento Marketplace account and generate authentication keys — these are required to pull packages from Magento’s Composer repository.
Prerequisites:
Update the system and install unzip:
bashsudo apt update && sudo apt upgradesudo apt install unzip
Create the MySQL database:
bashsudo mysql
sqlCREATE DATABASE magento;GRANT ALL ON magento.* TO 'magento'@'localhost' IDENTIFIED BY 'change-with-strong-password';EXIT;
Create a system user. Magento runs better under a dedicated account rather than the default www-data user. This gives you precise control over file permissions and PHP-FPM process isolation:
bashsudo useradd -m -U -r -d /opt/magento magentosudo usermod -a -G magento www-datasudo chmod 750 /opt/magento
Install PHP 7.2 with all modules Magento requires:
bashsudo apt install php7.2-common php7.2-cli php7.2-fpm php7.2-opcache php7.2-gd php7.2-mysql php7.2-curl php7.2-intl php7.2-xsl php7.2-mbstring php7.2-zip php7.2-bcmath php7.2-soap
Apply the required PHP settings via sed:
bashsudo sed -i "s/memory_limit = .*/memory_limit = 1024M/" /etc/php/7.2/fpm/php.inisudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 256M/" /etc/php/7.2/fpm/php.inisudo sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.2/fpm/php.inisudo sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.2/fpm/php.inisudo sed -i "s/;opcache.save_comments.*/opcache.save_comments = 1/" /etc/php/7.2/fpm/php.ini
Create a dedicated PHP-FPM pool for the magento user at /etc/php/7.2/fpm/pool.d/magento.conf. The pool should define user = magento, group = www-data, and listen = /var/run/php/php7.2-fpm-magento.sock. Restart PHP-FPM to activate it:
bashsystemctl restart php7.2-fpm
Install Composer globally to manage Magento’s dependencies:
bashcurl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
Switch to the magento user:
bashsudo su - magento
Download Magento from the official Composer repository. Enter your Marketplace access keys when prompted. Type Y to store them in auth.json so future updates do not require re-entering them:
bashcomposer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition /opt/magento/public_html
Install Magento from the command line. Replace the example domain, admin details, and database password with your own values:
bashcd ~/public_htmlphp bin/magento setup:install \ --base-url=https://example.com/ \ --base-url-secure=https://example.com/ \ --admin-firstname="John" --admin-lastname="Doe" \ --admin-email="john@example.com" \ --admin-user="john" --admin-password="YourPassword123!" \ --db-name="magento" --db-host="localhost" \ --db-user="magento" --db-password="change-with-strong-password" \ --currency=USD --timezone=America/Chicago --use-rewrites=1
The process takes a few minutes. When complete, the installer prints [SUCCESS] and a unique admin URI like /admin_xxxxxx. Save that URI before closing the terminal.
Magento uses cron jobs for re-indexing, email sending, sitemap generation, and catalog updates. Create the Magento crontab as the magento user:
bashphp ~/public_html/bin/magento cron:install
Switch back to your sudo user and create an Nginx server block at /etc/nginx/sites-available/example.com. The config needs a fastcgi_backend upstream block pointing to unix:/var/run/php/php7.2-fpm-magento.sock, plus three server blocks for HTTP redirect, www redirect, and the main HTTPS block. The main block sets $MAGE_ROOT to /opt/magento/public_html and includes Magento’s own nginx.conf.sample file, which handles all routing and security rules internally.
Test and restart Nginx:
bashsudo nginx -tsudo systemctl restart nginx
Open your domain in a browser. The Magento storefront confirms a successful installation.
Magento 2 is now installed on your Ubuntu 18.04 server. Visit the Magento Developer Documentation to learn about themes, modules, and performance tuning. Leave a comment below if you run into any issues.