How to Install Magento on a Linux Server: Complete Step-by-Step Guide
Magento is a powerful eCommerce platform for building online stores with advanced catalog, checkout, payment, inventory, and customization capabilities. Unlike hosted eCommerce platforms, Magento can be installed on your own Linux server, giving you greater control over the hosting environment.
However, Magento requires several server-side components, including PHP, a supported database, Composer, a web server, a search engine, and appropriate PHP extensions.
This guide explains how to install Magento Open Source on a Linux server from start to finish.
Important: Magento requirements vary by release. Before starting, verify that the PHP, database, OpenSearch/Elasticsearch, Composer, and other versions in this guide are compatible with the exact Magento release you intend to install.
What You Need Before Installing Magento
Before beginning, make sure you have:
- A Linux VPS or dedicated server
- Root or sudo access
- A domain name
- A supported PHP version
- MySQL or another supported database
- Apache or Nginx
- Composer
- OpenSearch or Elasticsearch, depending on the Magento release
- SSL certificate
- Sufficient RAM and storage
- A Magento authentication key pair
Magento authentication keys are used by Composer to access Magento packages. You can obtain them through your Magento/Adobe account.
Step 1: Update the Linux Server
Start by updating the operating system.
For Ubuntu or Debian-based systems:
sudo apt update
sudo apt upgrade -y
For RHEL-based distributions, use the appropriate package manager for your operating system.
After updating, reboot if the system recommends it:
sudo reboot
Reconnect to the server after it has restarted.
Step 2: Install Basic Server Utilities
Install commonly required utilities:
sudo apt install -y curl wget unzip git vim ca-certificates
You may also need additional packages depending on your Magento version and chosen web server.
Step 3: Install the Required PHP Version
Magento requires a specific PHP version depending on the release.
Check the currently installed version:
php -v
If PHP isn’t installed, install the version supported by your Magento release.
For example, on a supported Ubuntu environment, you may install PHP and common extensions with:
sudo apt install -y php php-cli php-fpm php-mysql php-curl php-gd \
php-intl php-mbstring php-xml php-zip php-bcmath php-soap
Then verify:
php -v
And:
php -m
Why PHP Compatibility Matters
Installing a PHP version that Magento does not support can result in:
- Installation failures
- Composer dependency errors
- Runtime errors
- Extension compatibility problems
- Unexpected application behavior
Always check the requirements for your exact Magento release before installing PHP.
Step 4: Install MySQL
Magento requires a supported database.
Install MySQL:
sudo apt install -y mysql-server
Start the service:
sudo systemctl enable --now mysql
Check its status:
sudo systemctl status mysql
Then run the security configuration:
sudo mysql_secure_installation
Follow the prompts to configure your MySQL installation securely.
Step 5: Create a Magento Database
Log in to MySQL:
sudo mysql
Create a database:
CREATE DATABASE magento;
Create a database user:
CREATE USER 'magento_user'@'localhost' IDENTIFIED BY 'StrongPasswordHere';
Grant privileges:
GRANT ALL PRIVILEGES ON magento.* TO 'magento_user'@'localhost';
Apply the privileges:
FLUSH PRIVILEGES;
Exit MySQL:
EXIT;
Keep the database name, username, and password available because you will need them during Magento installation.
Step 6: Install Composer
Composer manages Magento’s PHP dependencies.
Download the Composer installer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Install Composer globally:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Remove the installer:
rm composer-setup.php
Check the installation:
composer --version
Use a Composer version supported by your Magento release.
Step 7: Create the Magento Directory
Create a directory for the website:
sudo mkdir -p /var/www/magento
Change ownership:
sudo chown -R $USER:$USER /var/www/magento
Move into the directory:
cd /var/www/magento
Step 8: Configure Magento Authentication Keys
Magento packages are distributed through the Magento Composer repository.
You need authentication keys to install Magento.
When Composer asks for credentials:
- Username: Your Magento public key
- Password: Your Magento private key
You can obtain these credentials from your Magento account.
Do not publish your private authentication key or store it in publicly accessible files.
Step 9: Download Magento Using Composer
From the Magento directory, use Composer to create the project.
The exact package and version depend on the Magento release you want to install.
A typical command looks like:
composer create-project --repository-url=https://repo.magento.com/ \
magento/project-community-edition .
Composer will download Magento and its dependencies.
The process can take several minutes depending on your server and network connection.
Step 10: Configure Magento File Permissions
Magento needs appropriate permissions for its application files.
A common ownership configuration is:
sudo chown -R www-data:www-data /var/www/magento
The exact web-server user depends on your Linux distribution and web server configuration.
For Ubuntu with Apache or Nginx, www-data is commonly used.
Avoid using:
chmod -R 777
as a solution for permission problems. Overly permissive permissions can create security risks.
Step 11: Configure PHP Settings
Magento may require PHP configuration changes depending on the version and workload.
Edit the appropriate PHP configuration file:
sudo nano /etc/php/*/fpm/php.ini
Common settings that may need adjustment include:
memory_limit = 2G
max_execution_time = 1800
max_input_time = 1800
upload_max_filesize = 64M
post_max_size = 64M
The exact recommended values depend on the Magento release and workload.
After changing PHP-FPM settings, restart PHP-FPM:
sudo systemctl restart php*-fpm
Step 12: Configure Nginx or Apache
Magento requires a properly configured web server.
Nginx
Create a virtual host configuration for your domain.
For example:
server {
listen 80;
server_name example.com www.example.com;
set $MAGE_ROOT /var/www/magento;
set $MAGE_MODE production;
include /var/www/magento/nginx.conf.sample;
}
The actual configuration should be adapted to your Magento version and server setup.
Enable the configuration and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
Apache
If using Apache, enable the necessary modules and configure a virtual host pointing to Magento’s pub directory.
A simplified example is:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/magento/pub
<Directory /var/www/magento/pub>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Then test the configuration:
sudo apachectl configtest
Restart Apache:
sudo systemctl restart apache2
Magento’s pub directory should be used as the web-accessible document root rather than exposing the entire Magento installation.
Step 13: Install and Configure OpenSearch or Elasticsearch
Magento’s search functionality requires a compatible search engine.
Depending on your Magento version, you may need OpenSearch or Elasticsearch.
Check the requirements for your specific Magento release before installing either one.
After installing the supported search service, verify that it is running.
For example:
sudo systemctl status opensearch
You should also test the service locally:
curl http://127.0.0.1:9200
The exact configuration depends on the supported search engine and Magento release.
Step 14: Run the Magento Installation Command
Once the server, database, PHP, web server, and search engine are ready, run the Magento installer.
A typical command looks like:
php bin/magento setup:install \
--base-url=https://example.com/ \
--db-host=localhost \
--db-name=magento \
--db-user=magento_user \
--db-password='StrongPasswordHere' \
--admin-firstname=Admin \
--admin-lastname=User \
[email protected] \
--admin-user=admin \
--admin-password='StrongAdminPasswordHere' \
--language=en_US \
--currency=USD \
--timezone=Asia/Kolkata \
--use-rewrites=1
Replace the example values with your actual configuration.
Important
Use a strong administrator password and avoid publishing installation commands containing real database or administrator credentials.
The exact installer options can vary between Magento versions, so check the documentation for your specific release if an option has changed.
Step 15: Configure Magento Cron
Magento uses cron jobs for many background operations.
From the Magento directory, run:
php bin/magento cron:install
Verify the cron configuration:
crontab -l
You should see Magento-related cron entries.
Cron handles background operations such as indexing and scheduled tasks.
Step 16: Set Magento to Production Mode
For a production website, Magento should normally run in production mode.
Check the current mode:
php bin/magento deploy:mode:show
Set production mode:
php bin/magento deploy:mode:set production
Production mode is intended for live environments and provides a configuration optimized for production operation.
Step 17: Run Magento Indexers
Magento uses indexes to improve the performance of catalog and other operations.
Run:
php bin/magento indexer:reindex
Check indexer status:
php bin/magento indexer:status
If an indexer requires updating, configure Magento’s cron and indexing strategy appropriately.
Step 18: Flush Magento Cache
Clear the Magento cache:
php bin/magento cache:flush
Check cache status:
php bin/magento cache:status
You can enable disabled caches with:
php bin/magento cache:enable
Step 19: Configure SSL
An eCommerce store should use HTTPS.
If you are using Let’s Encrypt, Certbot can be used on supported Linux distributions.
For example:
sudo apt install certbot python3-certbot-nginx
Then:
sudo certbot --nginx -d example.com -d www.example.com
If using Apache, use the appropriate Certbot Apache package and command.
After enabling SSL, make sure Magento’s base URLs use HTTPS.
Step 20: Test Your Magento Store
Open your domain in a browser:
https://example.com
Then access the Magento administration area using the administrator URL configured during installation.
Test:
- Homepage
- Product pages
- Categories
- Search
- Customer registration
- Login
- Shopping cart
- Checkout
- Payment method
- Shipping method
- Order placement
- Transaction emails
- Admin dashboard
Testing the complete purchase process before launch is essential.
Common Magento Installation Problems
1. Composer Authentication Error
If Composer reports authentication problems, check that you are using valid Magento authentication keys.
Make sure the keys are associated with an account that has the required access.
2. PHP Version Error
If Magento reports that your PHP version is unsupported, install a PHP version compatible with your Magento release.
Check:
php -v
Also remember that CLI PHP and PHP-FPM can use different configurations.
3. Memory Limit Error
If Composer or Magento runs out of memory, check:
php -i | grep memory_limit
Increase the memory limit where appropriate and ensure the server itself has sufficient RAM.
4. Permission Errors
Incorrect ownership or permissions can cause errors involving:
vargeneratedpub/staticpub/media
Check the ownership and permissions of the Magento installation.
Avoid solving these problems by assigning 777 permissions to the entire Magento directory.
5. Search Engine Connection Error
If Magento cannot connect to OpenSearch or Elasticsearch, verify:
sudo systemctl status opensearch
Then test:
curl http://127.0.0.1:9200
Also verify that the search engine version is compatible with your Magento release.
6. 404 Errors
If Magento pages return 404 errors, check:
- Web-server configuration
- URL rewrites
- Magento base URL
- Document root
- Nginx/Apache configuration
- File permissions
For Apache, ensure the required rewrite functionality is enabled.
Magento Installation Checklist
Before considering the installation complete, verify:
- Linux server is updated
- Supported PHP version installed
- Required PHP extensions installed
- Supported database configured
- Composer installed
- Magento authentication keys configured
- Magento downloaded successfully
- Correct file ownership configured
- Nginx or Apache configured
- Supported OpenSearch/Elasticsearch configured
- Magento database created
- Magento installation completed
- Cron configured
- Indexers working
- Cache enabled
- HTTPS enabled
- Admin login tested
- Checkout tested
- Backups configured
Final Thoughts
Installing Magento on a Linux server requires more preparation than installing a simple PHP website because Magento depends on several services working together. PHP, the database, Composer, the web server, search engine, cron, caching, permissions, and SSL must all be configured correctly.
The most important step is to start with the exact Magento version you plan to install and verify its official system requirements before choosing PHP, database, and search-engine versions.
Once the base installation is working, focus on security, caching, backups, monitoring, and performance before opening the store to customers.