How to Install Magento with Nginx on Ubuntu Linux

|

|

This tutorial guides students and new users through the process of installing and using Adobe Commerce (previously Magento), a free and open-source eCommerce platform, on Ubuntu Linux with an Nginx HTTP web server. The steps include setting up Nginx, MariaDB database server, and PHP modules, creating a Magento database, downloading Magento 2, and configuring Nginx…

This brief post shows students and new users steps to install and use Adobe Commerce (previously Magento) on Ubuntu Linux with an Nginx HTTP web server.

This post will also have a link to set up free Let’s Encrypt SSL certificates to secure your Magento websites and applications.

Magento is a free and open-source eCommerce platform based on PHP and MySQL that millions of small businesses use to sell and manage their products online. If you want to create an online store, Magento might be the simplest way to do it, especially if you need support from users to manage and maintain the store.

Magento enables users to create a complete online store, including inventory management, product catalogs, shipping, invoicing, etc.

This tutorial is based on Ubuntu Linux. We’ll install the Nginx web server, MariaDB database server, and PHP modules. We’ll also link to another post showing how to secure your Magento website using Let’s Encrypt free SSL certificates.

For more about Magento, please check its homepage.

To get started with installing Magento on Ubuntu Linux, follow the steps below:

How to install Nginx on Ubuntu Linux

As mentioned above, we will use the Nginx web server to run Magento. Magento requires a web server to function, and Nginx is one of the most popular open-source web servers available today.

To install Nginx on Ubuntu, run the commands below:

sudo apt update
sudo apt install nginx

After installing Nginx, the commands below can stop, start, and enable Nginx services to start every time your server starts up.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

To test whether Nginx is installed and functioning, open your web browser and browse to the server’s IP address or hostname.

http://localhost

If you see the above page in your browser, Nginx works as expected.

How to install MariaDB on Ubuntu Linux

A database server is required for Magento to function. Magento stores its content in a database, and MariaDB is probably the best database server available to run Magento.

MariaDB is fast and secure and is the default server for almost all Linux servers. To install MariaDB, run the commands below:

sudo apt install mariadb-server
sudo apt install mariadb-client

After installing MariaDB, the commands below can stop, start, and enable MariaDB services to start up when the server boots.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation.

sudo mysql_secure_installation

When prompted, use the guide below to answer:

If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): PRESS ENTER

Switch to unix_socket authentication [Y/n] n

Change the root password? [Y/n] n

Remove anonymous users? [Y/n] y

Disallow root login remotely? [Y/n] y

Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y

All done!

To verify and validate that MariaDB is installed and working, log in to the database console using the commands below:

sudo mysql -u root -p

You should automatically be logged in to the database server since we initiated the login request as root. Only the root can log in without a password and from the server console.

The server was successfully installed if you see a similar screen.

How to install PHP-FPM on Ubuntu Linux

As mentioned above, we’re installing PHP on Ubuntu since Magento requires it. PHP packages are added to Ubuntu repositories. The versions of the repositories might not be the latest. If you need to install the latest versions, you’ll need to add a third-party PPA repository.

Run the commands below to a third-party repository with the latest versions of PHP.

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

At the time of this writing, the latest PHP version is 8.0.

sudo apt update

Next, run the commands below to install PHP 8.0 and related modules.

sudo apt install php8.0-fpm php8.0-common php8.0-mysql php8.0-gmp php8.0-curl php8.0-intl php8.0-mbstring php8.0-xmlrpc php8.0-gd php8.0-xml php8.0-cli php8.0-zip php8.0-soap php8.0-bcmath

Once PHP is installed, the commands below can start, stop, and enable PHP-FPM services to start when the server boots automatically.

sudo systemctl stop php8.0-fpm
sudo systemctl start php8.0-fpm
sudo systemctl enable php8.0-fpm

Next, you’ll want to change some great PHP configuration settings with Magento. Run the commands below to open the PHP default configuration file.

sudo nano /etc/php/8.0/fpm/php.ini

Then, change the line settings to something like the lines below. Save your changes and exit.

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
cgi.fix_pathinfo = 0
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

How to create a Magento database on Ubuntu

At this point, we’re ready to create a Magento database. As mentioned above, Magento uses databases to store its content.

To create a database for Magento, run the commands below:

sudo mysql -u root -p

Then, create a database called magentodb

CREATE DATABASE magentodb;

Next, create a database user called magentodbuser and set a password

CREATE USER 'magentodbuser'@'localhost' IDENTIFIED BY 'new_password_here';

Then, grant the user full access to the database.

GRANT ALL ON magentodb.* TO 'magentodbuser'@'localhost' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

How to download Magento 2

We’re ready to download Magento and begin configuring it. First, run the commands below to download the latest version of Magento from its repository.

You may want to use the GitHub repository to get Magento’s latest release. Install Composer, Curl, and other dependencies to get started.

sudo apt install curl git
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

After installing curl and Composer above, change it into the Nginx root directory and download the Magento 2 packages from GitHub.

When prompted, enter your authentication keys. Your public key is your username; your private key is your password….  ( https://marketplace.magento.com

You’ll have to register for an account to create the key above.

Then, run the command below to allow the www-data user to own the Magento directory.

Run the commands below to create a new project called Magento.

cd /var/www/
sudo composer create-project --repository=https://repo.magento.com/ magento/project-community-edition magento

Copy and paste the authentication key. (Your public key is your username; your private key is your password)

Output:
Authentication required (repo.magento.com):
Username: 234f2343435d190983j0ew8u3220
Password: 
Do you want to store credentials for repo.magento.com in /opt/magento/.config/composer/auth.json ? [Yn] Y

After downloading Magento packages, run the commands below to install Magento with the following options:

cd /var/www/magento
sudo bin/magento setup:install --base-url-secure=https://example.com/ --db-host=localhost --db-name=magentodb --db-user=magentodbuser --db-password=db_user_password_here --dbadmin-firstname=Admin --admin-lastname=User --admin-email=admin@example.com --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1
  • The Magento software is installed in the root directory on localhost. Admin is admin;  therefore: Your storefront URL is https://exmaple.com
  • The database server is on the same localhost as the webserver….
  • The database name is magento, and the magentouser and password is db_user_password_here
  • Uses server rewrites
  • The Magento administrator has the following properties:
    • The first and last name are: Admin User
    • Username is: admin
  •  and the password is admin123
  • E-mail address is: admin@example.com
  • Default language is: (U.S. English)
  • Default currency is: U.S. dollars
  • Default time zone is: U.S. Central (America/Chicago)

After that, run the commands below to set the correct permissions for Magento 2 to function.

sudo chown -R www-data:www-data /var/www/magento/
sudo chmod -R 755 /var/www/magento/

How to configure Nginx for Magento

We have downloaded Magento content into a new folder we called Magento. Now, let’s configure Nginx to create a new server block for our Magento website. You can create as many server blocks with Nginx as you want.

To do that, run the commands below to create a new configuration file called Magento.conf in the /etc/nginx/sites-available/ directory to host our Magento server block.

sudo nano /etc/nginx/sites-available/magento.conf

In the file, copy and paste the content below into the file and save.

upstream fastcgi_backend {
server unix:/var/run/php/php8.0-fpm.sock;
}

server {
    listen 80;
    listen [::]:80;
    root /var/www/magento;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    client_max_body_size 100M;
    autoindex off;

    set $MAGE_ROOT /var/www/magento;
    set $MAGE_MODE production;

    access_log /var/log/nginx/example.com-access.log;
    error_log /var/log/nginx/example.com-error.log;

    include /var/www/magento/nginx.conf.sample;


}

Save the file and exit.

After saving the file above, run the commands below to enable the new file that contains our Magento server block. Restart Nginx after that.

sudo ln -s /etc/nginx/sites-available/magento.conf /etc/nginx/sites-enabled/
sudo systemctl restart nginx.service

At this stage, Magento is ready and can be launched by going to the server’s IP or hostname.

http://localhost

However, we want to protect our server with Let’s Encrypt free SSL certificates. So, continue below to learn how to generate a Let’s Encrypt SSL certificate for websites.

How to setup Let’s Encrypt for Magento

We have written a great post on generating and managing Let’s Encrypt SSL certificates for the Nginx web servers. You can use that post to apply it here for your Magento website.

To read the post on how to generate Let’s Encrypt SSL certificates for a website, click on the link below:

How to Setup Let’s Encrypt on Ubuntu Linux with Nginx – Website for Students

If you successfully generate a Let’s Encrypt SSL certificate, you should reopen the server block for our Magento website by running the commands below.

sudo nano /etc/nginx/sites-available/magento.conf

The new Magento server block configurations should look similar to the line below. Take notes of the highlighted lines.

  • The first server block listens on port 80. It contains a 301 redirect to redirect HTTP to HTTPS.
  • The second server block listens on port 443. It contains a 301 redirect to redirect www to the non-www domain.
upstream fastcgi_backend {
server unix:/var/run/php/php8.0-fpm.sock;
}

server {
    listen 80;
    listen [::]:80;
    root /var/www/magento;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    include snippets/well-known.conf;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    root /var/www/magento;
    index  index.php index.html index.htm;
    server_name www.example.com;
   
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;

    add_header Strict-Transport-Security "max-age=31536000;  includeSubDomains";
    
    include snippets/well-known.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    root /var/www/magento;
    index  index.php index.html index.htm;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;

    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 30s;

    ssl_dhparam /etc/ssl/certs/dhparam.pem;

    add_header Strict-Transport-Security "max-age=31536000;  includeSubDomains";
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    
    include snippets/well-known.conf;

    client_max_body_size 100M;
    autoindex off;
    
    set $MAGE_ROOT /var/www/magento;
    set $MAGE_MODE production;

    access_log /var/log/nginx/example.com-access.log;
    error_log /var/log/nginx/example.com-error.log;

    include /var/www/magento/nginx.conf.sample;
}

Save the file above, then restart Nginx and PHP using the commands below.

sudo systemctl reload nginx
sudo systemctl reload php8.0-fpm

Finally, if everything went as planned, you should be able to start the Magento setup wizard by browsing to the server hostname or IP address over HTTPS.

https://example.com/

Next, open your browser and browse the server domain name. You should see the Magento homepage.

You can now log in as an administrator and start customizing and building your store.

How to upgrade Magento 2

First, stop the web server.

sudo systemctl stop nginx

In the future, when you want to upgrade to a newly released version, simply run the commands below to upgrade…

cd /var/www/magento
sudo bin/magento maintenance:enable
sudo composer require magento/product-community-edition 2.2.5 --no-update
sudo composer update
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento indexer:reindex
sudo php bin/magento maintenance:disable

You may have to re-run the to update Nginx directory permissions.

Conclusion:

This post showed you how to install Magento 2 on Ubuntu Linux with a link to set up LLet’sEncrypt. Please use the comment form below if you find any errors above or have something to add.

Like this:



4 responses to “How to Install Magento with Nginx on Ubuntu Linux”

  1. New-Magengo Avatar
    New-Magengo

    i’ve run command as below
    cd /var/www/
    sudo composer create-project –repository=https://repo.magento.com/ magento/project-community-edition magento

    and then i got message as this line
    Warning from repo.magento.com: Your Magento authentication keys are invalid. Please double-check your keys in your Marketplace account. For instructions, visit https://devdocs.magento.com/guides/v2.3/install-gde/prereq/connect-auth.html
    Warning from repo.magento.com: Your Magento authentication keys are invalid. Please double-check your keys in your Marketplace account. For instructions, visit https://devdocs.magento.com/guides/v2.3/install-gde/prereq/connect-auth.html
    Warning from repo.magento.com: Your Magento authentication keys are invalid. Please double-check your keys in your Marketplace account. For instructions, visit https://devdocs.magento.com/guides/v2.3/install-gde/prereq/connect-auth.html

    [Composer\Downloader\TransportException]
    Invalid credentials for ‘https://repo.magento.com/packages.json’, aborting.

    How i can fix it . Thank you

    1. Richard Avatar
      Richard

      You’ll want to follow the post correctly. You need to create an account and use the key to install.

  2. Adrian Stewart Avatar
    Adrian Stewart

    Hi,

    I am receiving an error ” nginx: [emerg] “root” directive is duplicate in /var/www/html/nginx.conf.sample:32
    nginx: configuration file /etc/nginx/nginx.conf test failed

    what could that be?

    1. hisam Avatar
      hisam

      i got same problem too..

Leave a Reply to Adrian Stewart Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.