Dec 02, 2025
Ariffud M. & Valentinas C.
8min Read
Known as a powerful open-source eCommerce platform, Magento 2 offers flexibility and extensive features to drive success for online stores. However, installing Magento 2 can be quite challenging for some users due to its rather tricky technical demands.
In this article, you’ll learn how to install Magento 2 on a Linux VPS, both through a VPS template and a manual approach. By practicing our clear and easy-to-follow Magento 2 installation guide, you’ll be one step closer to launching your online store.
Download ultimate SSH commands cheat sheet
Before setting up Magento 2 on a Linux server, it’s essential to have a few key components ready, such as:
Installing Magento 2 via Hostinger’s VPS template is the easiest way to build your online store. Available on all our VPS plans, this method simplifies the entire setup process by including all necessary software and configurations right from the start. For more details, visit our Magento Hosting page.
If you use Hostinger VPS, you can ask Kodee AI assistant to check if your server is compatible and meets the Magento template minimum requirement.
Follow these steps to install the Magento 2 template on Hostinger’s VPS:



Wait for the installation process to finish. It shouldn’t take more than 10 minutes. Once done, go to Overview → Application access and click the login URL provided to open Magento 2’s dashboard.

After successfully logging in to the admin panel, you can follow our Magento 2 tutorial, which covers setting up basic information, customizing your online store’s design, adding products to your site, and utilizing Magento’s advanced features.

This section will guide you through setting up Magento 2 manually on an Ubuntu 24.04 distribution using Linux commands. If you use other distributions, such as CentOS, the commands may differ slightly.
Before installing Magento on Linux, it’s essential to update your VPS packages. This step ensures compatibility with Magento requirements and enhances your server’s security and performance.
Follow these steps to update your Linux VPS packages:
ssh username@ip_address
sudo apt update
sudo apt upgrade -y
sudo apt autoremove
After your system is updated, the next step is to install the Apache web server to host your Magento store. You’ll also configure it to start automatically and set up a virtual host for Magento.
Here are the instructions:
sudo apt install apache2 -y
sudo systemctl enable apache2.service
sudo nano /etc/apache2/sites-available/magento2.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /var/www/html/magento2/pub
</VirtualHost>
sudo a2ensite magento2.conf
sudo systemctl restart apache2.service
sudo a2enmod rewrite
sudo a2enmod headers
sudo systemctl restart apache2.service
Now, it’s time to install a database management system (DBMS) and create a database for your Magento store. Here, we’re going for MariaDB instead of MySQL due to its better performance and adaptation to Magento’s high demands.
Below are the steps for installing MariaDB and creating a database:
sudo apt install mariadb-server mariadb-client -y
sudo systemctl restart mariadb.service
sudo systemctl enable mariadb.service
sudo mysql_secure_installation
sudo mysql -u root -p
CREATE DATABASE magento_db;
CREATE USER magento_user@localhost IDENTIFIED BY your password;
GRANT ALL ON magento_db.* TO magento_user@localhost IDENTIFIED BY your password WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
Let’s move on to the fourth step, installing PHP and its required extensions. PHP is the core scripting language that Magento runs on, and properly configuring it is crucial for optimal performance.
Follow this guide:
sudo apt install php8.1 libapache2-mod-php8.1 php8.1-common php8.1-gmp php8.1-curl php8.1-soap php8.1-bcmath php8.1-intl php8.1-mbstring php8.1-xmlrpc php8.1-mysql php8.1-gd php8.1-xml php8.1-cli php8.1-zip -y
sudo nano /etc/php/8.1/apache2/php.ini
file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 512M
upload_max_filesize = 128M
max_execution_time = 3600
sudo systemctl restart apache2.service
Magento 2 requires a powerful search engine for handling complex product searches and catalog management efficiently. Therefore, you need to install Elasticsearch, which provides fast and accurate search results for your online store.
To install Elasticsearch, follow these steps:
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
sudo apt update
sudo apt install elasticsearch
sudo systemctl start elasticsearch.service
sudo systemctl enable elasticsearch.service
sudo nano /etc/elasticsearch/elasticsearch.yml
xpack.security.enabled: false
systemctl restart elasticsearch.service
curl -X GET "localhost:9200/"
Here’s the output you should see:
{
"name": "srv123456",
"cluster_name": "elasticsearch",
"cluster_uuid" : "gVGFaOTqRSa6HvNz1PX28g",
"version": {
"number": "7.17.20",
"build_flavor": "default",
"build_type": "deb",
"build_hash" : "b26557f585b7d95c71a5549e571a6bcd2667697d",
"build_date" : "2025-01-08T08:34:31.070382898Z",
"build_snapshot": false,
"lucene_version" : "8.11.3",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}The next crucial step is to install Composer on your VPS. Composer is a dependency manager for PHP that Magento 2 uses to manage both official and third-party libraries and their packages.
Follow these steps to download and install Composer:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer --version
This command should display the installed Composer version. For example:
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ / __ `__ / __ / __ / ___/ _ / ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
____/____/_/ /_/ /_/ .___/____/____/___/_/
/_/
Composer version 2.7.2 2024-03-11 17:12:18
With Composer set up and all prerequisites in place, you are now ready to run the Magento installation command. This will configure your Magento store, link it to the necessary database, and set up the admin account.
Here are the steps to install Magento 2:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition /var/www/html/magento2
cd /var/www/html/magento2
sudo chown -R www-data:www-data /var/www/html/magento2/
sudo chmod -R 755 /var/www/html/magento2/
php bin/magento setup:install
--base-url=http://yourdomain.com
--db-host=localhost
--db-name=magento_db
--db-user=magento_user
--db-password=your_password
--admin-firstname=Admin
--admin-lastname=User
--admin-email=admin@example.com
--admin-user=adminuser
--admin-password=admin123
--language=en_US
--currency=USD
--timezone=America/New_York
--use-rewrites=1

Congratulations, you’ve successfully installed Magento 2 on your VPS hosting environment using the command-line operation. However, if you can’t access the Magento server after installation, scroll down to the troubleshooting section.
One of the most common post-installation issues of Magento 2 is facing a 404 error on the admin page. This error indicates problems with file permissions or web server configurations. Here are several troubleshooting methods to resolve this issue:
Correct File Permissions
Ensure that the var, pub, and generated directories have the proper permissions.
cd /var/www/html/magento2
chmod -R 777 var pub generated
Replace .htaccess Files
Sometimes, .htaccess files may be corrupt or improperly generated. Therefore, you need to remove and recreate the .htaccess files in the Magento root and pub directories.
rm .htaccess
sudo nano .htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule .* /pub/$0 [L]
DirectoryIndex index.php
cd pub
Allow Override in Apache Configuration
The Apache configuration must allow overrides for .htaccess files to function properly.
sudo nano /etc/apache2/apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
sudo systemctl restart apache2
Run Magento Commands
Sometimes, running specific Magento commands can resolve residual configuration issues.
php bin/magento cache:clean
php bin/magento setup:static-content:deploy
In this article, you’ve learned how to install Magento 2 on a Linux VPS using both automated and manual approaches. Regardless of the method you settled on, you’re now equipped to start your online store with the Magento open-source platform.
Keep learning and exploring Magento 2 further to enhance your online store’s functionality and customer experience. Your next steps could redefine your eCommerce success.
This section will answer the most common questions on how to install and configure Magento 2.
While it’s technically possible to install Magento 2 on shared web hosting, the platform’s resource-intensive nature makes a VPS or dedicated server preferable for optimal performance.
The Magento installation process using Hostinger’s VPS template typically takes about 10 minutes. This quick setup time includes loading the Ubuntu 24.04 OS with pre-configured Magento settings.
Yes, you should get an SSL certificate for Magento 2. It secures data transactions, protects user information, and boosts customer trust, which is critical for maintaining a secure and reliable shopping experience.
Comments
August 29 2017
Hi I installed Magento 2.1.8 in a public_html subfolder mydomain.com/store as i already have a live store and was planning to build a new site to test, but how do I access Magento Admin to do the setting. What URL would it have?