Skip to content
Follow
CMS

How to Install Magento with Apache on Ubuntu 24.04

Richard
Written by
Richard
May 31, 2024 Updated Oct 13, 2026 11 min read
Magento featured image
Magento featured image

Magento 2.4.7 or later with Apache on Ubuntu 24.04 gives you a working online store on your own server. Magento is free e-commerce software for building and running an online shop. Apache is the web server that puts the store on the internet so customers can visit it.

Advertisement

The setup has five parts. You install Apache, MariaDB (the database) and PHP. You then download and install Magento, and finally point Apache at the store and add HTTPS. You don’t need advanced skills, but you do need a terminal on the server and an account that can use sudo. Run the steps in order, because each part depends on the one before it. Plan for a fresh Ubuntu 24.04 server, since a server that already runs other sites may conflict with these settings.

⚡ Quick Answer

Install Magento with Apache on Ubuntu 24.04 by installing Apache, MariaDB and PHP first, then downloading Magento and pointing Apache at the store. Start with sudo apt update and sudo apt install apache2, then enable Apache with sudo systemctl enable apache2 so the store comes back after a reboot. Run the steps in order on a fresh server.

Advertisement

Install Apache HTTP server on Ubuntu Linux

You install the Apache HTTP server on Ubuntu by running the apt update and apt install apache2 commands in the terminal. Apache is the web server software that receives requests from browsers and serves your Magento store pages. Nothing else in this guide works until it is running.

Open the Ubuntu terminal and run the commands below to install the Apache web server. The first command refreshes the package list so Ubuntu installs the current version. The second command installs Apache.

🐧Bash / Shell
sudo apt update
sudo apt install apache2
📝Good to KnowOnce Apache is installed, use the commands below to start it, stop it, and enable it so it starts automatically when the server boots.

Enabling it matters. Without it, your store goes offline after every reboot until someone starts Apache by hand.

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

To test that Apache is running, open a web browser and browse to the server’s localhost or IP address. Use localhost if the browser is on the server itself. Use the server’s IP address if you are testing from another computer.

Advertisement

http://localhost

Apache2 default landing page confirming successful web server installation for Magento
Apache2 default landing page confirming successful web server installation for Magento

When you see the Apache2 Default Page, the Apache HTTP server is installed and working. If the page doesn't load from another computer, check that the server's firewall, or your hosting provider's firewall, allows web traffic on ports 80 and 443.

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

How to install Apache on Ubuntu

Advertisement

Install MariaDB database server on Ubuntu Linux

You install the MariaDB database server on Ubuntu by running the standard package manager commands in the terminal. MariaDB is the database backend Magento needs. It stores your products, customer orders and site settings, so Magento cannot install without it.

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

Open the Ubuntu terminal and 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. Enabling MariaDB makes it start automatically when the server boots, so Magento can always reach its data after a reboot.

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

Run the following commands to check that the MariaDB database server is installed and responding.

🐧Bash / Shell
sudo mariadb

Once you run the commands above, you are logged on to the MariaDB console. It displays 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)]> 

The message tells you the server is installed and accepting connections. If you don't see it, the service isn't running, so go back to the start, stop and enable commands above and check them for errors.

Additional help on installing MariaDB.

Advertisement

Create a Magento database

You create the Magento database from the MariaDB command-line interface. This gives Magento its own dedicated, isolated space to save catalog items, user accounts and configuration details, separate from anything else on the server.

The Magento database is the central storage for all Magento application content and data. The Magento setup process needs it, and the installer stops with an error if it can't connect.

In this setup, you create a database named ‘magentodb ‘and a matching user account named ‘magentodbuser ‘. Using a dedicated user, rather than the MariaDB root account, limits the damage if the store's credentials are ever exposed.

The magentodbuser account then receives full access to the magentodb database. Magento needs this to create its tables and run correctly.

Advertisement

You can do all of the database steps above with the commands below. Do it in two parts:

First, log on to the MariaDB database server:

🐧Bash / Shell
sudo mariadb

Then run the commands below at the MariaDB prompt to complete the steps:

💻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 KnowReplace ‘type_your_password_here‘ with your own strong password before you run the command.

Write the password down, because you need the same one later when you install Magento.

Advertisement

Install PHP on Ubuntu Linux

To install PHP on Ubuntu for Magento, you add the core PHP language and the extensions Magento needs, such as php-intl, php-mysql and php-common, with the apt package manager. These modules let the server run Magento's code and connect to the database. If an extension is missing, the Magento installer reports it and stops. Install the missing package and run the installer again.

Run the commands below to install PHP.

🐧Bash / Shell
sudo apt install php libapache2-mod-php php-intl php-mysql php-curl php-cli php-zip php-gd php-common php-mbstring 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

To download Magento onto your server, you first install curl and git, then Composer, the dependency manager that fetches the Magento source code. This prepares your Ubuntu environment by pulling all the core files into your web directory before the final setup runs.

Run the command below to install these packages, then continue with the Magento installation.

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

Before 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. If you don’t have an account, click Register and create one.
  2. Click your account name in the top right of the page.
  3. Select My Profile. Your profile page opens.

Click Access Keys in the Marketplace tab. The page that opens lists any access keys already on your account.

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 your store or server.
  3. Click OK. The new public and private keys appear on the page.

You can click to copy the new public and private keys associated with your account. Save this key information or keep the keys page open while you work on your project. You will paste both keys into the terminal shortly.

Composer asks for a username and password, so use the Public key as your username and the Private key as your password.

Your Apache root directory is where you will install the Magento project, so change to that directory first. Then run the command below. It clones the Magento project and creates a new site named "magento", or any other name you choose for your site. The download is large, so it can take several minutes.

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

When Composer asks for credentials, copy and paste the authentication keys. Your public key is your username, and your private key is your password. The terminal doesn't show anything as you paste the password. That is normal.

Enter the Username (your Access public key) and Password (your Access private key) from your Marketplace account. If you get an authentication error, check that you didn't copy the keys the wrong way round or include extra spaces.

💻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 download, run the commands below to install Magento with the following options. Change the example values to your own before you run them, especially the database password, the URL and the administrator details.

⬛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 --use-rewrites=1
  • The Magento software is installed in the root directory on localhost. Admin is admin; Your storefront URL is https://magento.examaple.com. Use your own domain name here.
  • 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. The password must match the one you set when you created the database user.
  • Uses server rewrites, which gives you clean URLs without index.php in them.
  • The Magento administrator has the following properties:
    • First and last name are: Admin User
    • Username is: admin
  •  and the password is admin123. This is only an example. Choose a strong password for a real store, because the admin account controls everything.
  • 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. This can take a few minutes, so let it finish. When it is done, you see a message similar to the one below. Copy the admin URI it shows, because you need it to reach the admin panel later.

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

Magento 2.4 needs a search engine to run. If the installer stops with a search-related error, or you have 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 before you go on. If it isn't running, Magento can't build its search index and the store may show errors.

Next, set the correct file permissions, generate the crontab, and run Magento indexing with the commands below. Together they make sure Magento's scheduled tasks and data processing work correctly. This step matters for site performance. Incomplete indexing can lead to slow loading times and display errors for your customers.

Finally, clear the cache of your Magento installation so the store starts with fresh data.

🐧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 Apache for Magento

You configure Apache for Magento by creating a new virtual host configuration file in the /etc/apache2/sites-available directory. This tells the web server where your store files live on disk and how to direct incoming visitors to your site. Without it, Apache keeps showing its default page instead of your store.

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

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

The command opens the new file in an editor. Copy the content block below and paste it into the file. Change the server name and the paths in it to match your own domain and Magento directory.

💻Code
<VirtualHost *:80>
ServerName magento.example.com
ServerAlias www.magento.example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/magento

<Directory /var/www/magento/>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Save the file and close the editor.

Enable the server block for your Magento website and restart the Apache web server. This makes Apache recognize the new configuration and start serving your website. Run the following commands: sudo a2ensite your_magento_site.conf && sudo systemctl restart apache2

🐧Bash / Shell
sudo a2ensite magento.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

Setup Let’s Encrypt SSL/TLS for Magento

Let's Encrypt SSL/TLS secures your checkout and customer data. You generate a free HTTPS certificate with the Certbot utility. This encrypts the traffic between your visitors and the Ubuntu server, which any live online store needs. Before you start, your domain name must point to the server's public IP address, and port 80 must be reachable from the internet. Let's Encrypt can't issue a certificate otherwise.

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

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

After installing, the Certbot Apache plugin automatically configures HTTPS in the Apache server block file /etc/apache2/sites-available/magento.conf. Certbot renews the certificate on its own, so you don't have to repeat this step every few months.

Once you have restarted the Apache web server, open your browser and browse to the server hostname or IP address defined in the Apache server block. Use the same name you entered as the storefront URL during the Magento installation.

💻Code
https://magento.example.com

The Magento default welcome page appears. If you still see the Apache2 Default Page, the new site isn't enabled yet, so check that you ran the enable command and restarted Apache.

Magento default welcome page
Magento default welcome page

That should do it!

Conclusion:

  • In this tutorial, you learned how to install and configure the Magento eCommerce platform with Apache on Ubuntu 24.04.
  • By following these steps, you have set up a solid and secure environment to run your e-commerce website. Before you go live, replace the example admin password with a strong one.
  • Apache, MariaDB and PHP together provide a stable foundation for hosting Magento.
  • The links above point to additional resources for setting up Let’s Encrypt SSL certificates for Apache to secure your Magento installation.

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 *