Skip to content
Follow
CMS

How to Install Magento with Nginx on Ubuntu 24.04

Richard
Written by
Richard
Updated 11 min read
Magento featured image
Magento featured image

Magento with Nginx on Ubuntu 24.04 gives you a fast online store that keeps up with heavy visitor traffic. Magento is a free e-commerce platform for running online shops, and Nginx is a high-speed web server that serves pages quickly and uses little memory.

Advertisement

Together with MariaDB and PHP, they make a secure setup with strong performance for your shoppers. Before you start, you need:

  • A server running Ubuntu 24.04.
  • Root access, or a user that can run commands with sudo.
  • Basic command-line knowledge.
  • A domain name that points to the server, if you want the HTTPS step at the end to work.
  • Enough memory. Magento and its search engine are heavy, and a very small server will struggle during the install.
⚡ Quick Answer

To install Magento with Nginx on Ubuntu 24.04, first install the web server by running sudo apt update, then sudo apt install nginx. Next, install MariaDB with sudo apt install mariadb-server, and enable both services with sudo systemctl enable nginx and sudo systemctl enable mariadb so they start after every reboot.

Advertisement

Install Nginx HTTP server on Ubuntu Linux

Nginx is the web server that loads your store pages for visitors, so it goes on first. You install it from the standard Ubuntu repositories. Update the package list with sudo apt update, then install the web server with sudo apt install nginx. The steps below walk through it, and the full commands are in the code blocks.

  1. Open the Ubuntu terminal. On a desktop, press Ctrl+Alt+T. On a remote server, connect over SSH.
  2. Run the commands below to install the Nginx web server.
  3. Watch the output. Apt lists the packages it will install, and the command finishes without an error message when it is done.
🐧Bash / Shell
sudo apt update
sudo apt install nginx

Once Nginx is installed, use the commands below to start it, stop it, and enable it. Enabling is the important part. It makes Nginx start by itself every time the server boots, so your store comes back after a reboot without you doing anything.

Advertisement

On a fresh install the service usually starts on its own. Running the start command again is harmless.

🐧Bash / Shell
sudo systemctl stop nginx
sudo systemctl start nginx
sudo systemctl enable nginx

To check that Nginx is running, open a web browser and go to the server’s localhost or IP address. If your server has no desktop, open the IP address from another computer on the same network or from the internet.

http://localhost

Nginx default welcome page on Ubuntu 24.04
Nginx default welcome page on Ubuntu 24.04

When you see the Welcome to nginx! page, the Nginx HTTP server is installed and working.

Advertisement

If the page does not load, check two things. First, make sure the Nginx service is running. Second, if you use a firewall, make sure it allows web traffic on port 80 (and port 443 later for HTTPS).

Additional help on installing Nginx on Ubuntu is in the link below.

How to install Nginx on Ubuntu

Install MariaDB database server on Ubuntu Linux

MariaDB stores all your store data: products, orders, and customer accounts. You install it from the Ubuntu package manager, then start it and check that it responds. Magento cannot run without a database, so do this before anything else Magento-related.

Advertisement

To install and use the MariaDB database server, use the instructions below.

  1. Open the Ubuntu terminal.
  2. Run the commands below to install the MariaDB database server.
🐧Bash / Shell
sudo apt update
sudo apt install mariadb-server

Once the MariaDB database server is installed, use the commands below to stop, start, and enable it. As with Nginx, enabling makes MariaDB start automatically when the server boots. If the database is down, your store shows errors, so you want this set.

🐧Bash / Shell
sudo systemctl stop mariadb
sudo systemctl start mariadb
sudo systemctl enable mariadb

Run the following commands to confirm that the MariaDB database server is installed and working.

Advertisement
🐧Bash / Shell
sudo mariadb

After you run the commands above, you are logged on to the MariaDB console and see a message similar to the one below.

💻Code
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 32
Server version: 10.11.2-MariaDB-1 Ubuntu 23.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> 

This message tells you the server is installed and accepting connections. Leave the console before you move on.

Additional help on installing MariaDB.

Advertisement

Create a Magento database

Next you create an empty database for Magento. You log in to the MariaDB shell and run a few SQL commands that create storage named magentodb. This database holds your catalog items, orders, and site settings. Write down the database name, the user name, and the password. You need all three for the Magento install later.

The Magento database is the central storage for all Magento application content and data, so it is a critical part of the setup.

As part of the setup, we will create a database named ‘magentodb ‘and a corresponding user account named ‘magentodbuser ‘.

The magentodbuser account receives full permissions on the magentodb database so that Magento can properly manage your store's data. It has no rights on any other database, which keeps the setup tidy.

Advertisement

All the database steps above can be done using the commands below:

But first, log on to the MariaDB database server:

🐧Bash / Shell
sudo mariadb

Then run the commands below to complete the steps. Each SQL statement ends with a semicolon, and MariaDB answers each one with a short confirmation.

💻Code
CREATE DATABASE magentodb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER magentodbuser@localhost IDENTIFIED BY 'type_your_password_here';
GRANT ALL ON magentodb.* TO magentodbuser@localhost WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit
📝Good to KnowMake sure you replace ‘type_your_password_here‘ with your own password.

Use a strong one, and remember it. The Magento install command later asks for this same password, so the two must match exactly.

Install PHP on Ubuntu Linux

Magento is a PHP application, so you need PHP and a set of extensions. The extensions, such as php-mysql and php-xml, let PHP run Magento's code and talk to the database. Install them with the apt package manager, using the full list of modules in one command. Leaving one out is a common cause of errors later.

Ubuntu 24.04 ships PHP 8.3 in its default repositories, and Magento 2.4.7 supports PHP 8.3. If you install a different Magento release, check its system requirements first to confirm the PHP version it supports.

Run the commands below to install PHP.

🐧Bash / Shell
sudo apt install php-fpm php-intl php-mysql php-curl php-cli php-zip php-gd php-common php-mbstring php-json php-sqlite3 php-zip php-soap php-bcmath php-xml

Additional help on installing PHP

How to install PHP on Ubuntu Linux

Download Magento files

You download the Magento files with Composer, a tool that manages PHP project software and its dependencies. First install curl and git, then install Composer. After that, Composer pulls the Magento core files into your web server directory.

Run the command below to install these packages and start installing Magento.

🐧Bash / Shell
sudo apt install curl git
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
📝Good to KnowBefore you can download Magento packages from its repository, you must create a new access key for Magento.

The key is free.

To create authentication keys:

  1. Log in to the Commerce Marketplace.
  2. If you don’t have an account, click Register and create one first.
  3. Click your account name in the top right of the page and select My Profile.
  1. Click Access Keys in the Marketplace tab. The page that opens lists any keys you already have.
Magento access key page
Magento access key page
  1. Click Create a New Access Key.
  2. Enter a specific name for the keys, for example the name of the server.
  3. Click OK. The new public and private keys now appear on the page.

You can click to copy the new public and private keys associated with your account. Save them, or keep the page open while you work on your project.

Use the Public key as your username and the Private key as your password.

With your access keys handy, change to the Nginx root directory. Then run the command below to download the Magento project and create a new site. Name the site "magento" or whatever you want to call it. If you pick a different name, use it everywhere the later steps say magento.

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

Composer then asks for your authentication keys. Copy and paste them from the Marketplace page. (Your public key is your username; your private key is your password).

Enter the Username (your Access public key) and Password (your Access private key) from your Marketplace account. The terminal may not show anything as you paste the password. That is normal. Composer may also ask whether to store the credentials. Answer as you prefer. Saving them means you won't be asked again on this server.

💻Code
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 the Magento packages finish downloading, run the commands below to install Magento with the following options:

⬛Command Prompt
cd /var/www/magento
sudo bin/magento setup:install --base-url-secure=https://magento.example.com/ --db-host=localhost --db-name=magentodb --db-user=magentodbuser --db-password=db_user_password_here --admin-firstname=Admin --admin-lastname=User --admin-email=admin@example.com --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/Chicago
  • The Magento software is installed in the root directory on localhost. Admin is admin; Your storefront URL is https://magento.examaple.com. That URL is only a placeholder from the example command. Replace it with your own domain.
  • The database server is on the same localhost as the webserver.
  • The database name is magentodb, and the magentodbuser and password are db_user_password_here. Put in the password you chose when you created the database.
  • Uses server rewrites
  • The Magento administrator has the following properties:
    • First and last name are: Admin User
    • Username is: admin
  •  and the password is admin123. This is an example only. Choose a strong password before you use the store for real.
  • 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)

During the installation, text moves quickly up the screen, and the install can take several minutes. Don't interrupt it. When it is done, you see a message similar to the one below. It also shows the Admin URI. Note it down, because Magento 2.4 can use a different admin path than /admin.

💻Code
[Progress: 1448 / 1448]
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_f97ul2i
Nothing to import.

Magento 2.4 needs a search engine running before the install can finish. If you run into trouble installing Elasticsearch, read the article below to learn how to install it on Ubuntu.

How to install Elasticsearch on Ubuntu Linux

Make sure the Elasticsearch service is started. If the install stopped with a search-related error, start the service and run the install again.

Correct file permissions, a crontab, and Magento indexing are essential for Magento's smooth operation. Run the commands below to set the permissions and generate the crontab, so Magento can run its scheduled jobs and process data efficiently. If pages later show errors about files that can't be written, wrong permissions are the first thing to check.

Finally, clear the cache of your Magento installation so it picks up all the changes.

🐧Bash / Shell
sudo chown -R www-data:www-data /var/www/magento/
sudo -u www-data bin/magento cron:install
sudo -u www-data bin/magento cron:run --group index
sudo -u www-data bin/magento cache:clean

Configure Nginx for Magento

Nginx needs a virtual host (server block) file for Magento. You create it in the sites-available directory with the nano text editor, run with sudo. The file tells Nginx where your store files are and how to handle requests for them.

Run the commands below to create an Nginx virtual host file for Magento.

🐧Bash / Shell
sudo nano /etc/nginx/sites-available/magento.conf

Then copy and paste the content block below into the Nginx server block. Before you save, check the values that are specific to your server: the server name must be your domain, the root path must point to your Magento folder, and the PHP-FPM address must match the PHP version you installed.

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

server {
listen 80;
listen [::]:80;
index index.php;
server_name magento.example.com www.magento.example.com;

set $MAGE_ROOT /var/www/magento;

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. In nano, press Ctrl+O, then Enter to confirm the file name, then Ctrl+X to exit.

Nginx server blocks are configurations that tell Nginx how to handle requests for different websites. Run the following commands to enable the server block for your Magento installation. Then restart Nginx so it loads the new configuration. If the restart fails, look for typos in the file you just saved, such as a missing semicolon or a wrong path.

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

Setup Let’s Encrypt SSL/TLS for Magento

A free Let's Encrypt SSL certificate lets you serve Magento over HTTPS, which protects your checkout and customer data. You install the Certbot plugin for Nginx on Ubuntu, and it requests the certificate and applies it for you. This encrypts traffic between your visitors and the web server. It only works if your domain already points to the server and ports 80 and 443 are open.

Please read the post below for additional resources on installing and creating Let’s Encrypt SSL certificates for Nginx.

How to set up Let’s Encrypt SSL certificate for Nginx on Ubuntu Linux

After it is installed, the Certbot Nginx plugin automatically configures the Nginx server block file /etc/nginx/sites-available/magento.conf with HTTPS. You don't need to edit the file by hand for this.

  1. Restart the Nginx web server.
  2. Open your browser.
  3. Browse to the server hostname or IP address defined in the Nginx server block.
💻Code
https://magento.example.com

The Magento default welcome page appears. If you see an Nginx error page instead, recheck the server block. If the admin login is not at /admin, use the Admin URI shown at the end of the install.

Magento default welcome page after installation
Magento default welcome page after installation

That should do it!

Conclusion:

Conclusion:

  • Installing Magento with Nginx support on Ubuntu 24.04 can significantly improve performance, resource utilization, and user experience.
  • Integrating Nginx, MariaDB, and PHP creates a robust foundation for efficiently hosting a Magento application.
  • The steps for setting up databases, creating a Magento database, installing PHP, downloading Magento files, and configuring Nginx are comprehensive and well-documented.
  • The addition of Let’s Encrypt SSL/TLS provides an essential layer of security for the Magento installation.
  • For any errors or additional information, the comments section is open for further discussion and support.

Have a question about this guide?

Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.

Did this guide fix your problem?

Richard

About the Author

Richard

Tech Writer, IT Professional

Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.

Advertisement

No comments yet — be the first to share your thoughts!

Leave a Comment

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