Magento is one of the most popular open-source e-commerce platforms available today. It offers a rich set of features that provide an unparalleled e-commerce experience. Magento is highly customizable, scalable, and designed to handle large amounts of data, making it ideal for businesses of all sizes. Whether you are a small business looking to expand your online presence or a large enterprise needing a robust platform to manage your sales, Magento has the tools to help you succeed.

How to Install Magento2 on Ubuntu

In this tutorial, we will walk you through the process of installing Magento on an Ubuntu server. By the end of this tutorial, you will have a fully functional Magento store up and running. We will cover everything from installing the necessary software to configuring your server and setting up Magento.

Step 1: Install Apache and PHP

First, we need to install Apache, PHP, and Composer. Apache is the web server, PHP is the scripting language, and Composer is a dependency manager for PHP.

  1. Open your terminal.
  2. Update your package list:
    sudo apt update
    
  3. Install Apache:
    sudo apt install apache2
    
  4. Install PHP and necessary PHP extensions:
    sudo apt install php libapache2-mod-php php-mysql php-xml php-curl php-intl php-soap php-zip php-gd php-bcmath php-mbstring
    
  5. Install Composer:
    sudo apt install composer
    

Step 2: Install and Configure MariaDB

MariaDB is the database server that Magento will use to store its data. You can also choose MySQL Server to use with your application.

  1. Install MariaDB:
    sudo apt install mariadb-server
    
  2. Secure the MariaDB installation:
    sudo mysql_secure_installation
    

    Follow the prompts to set the root password and remove unnecessary options for better security.

Step 3: Create Database and User

We need to create a database and a user for Magento.

  1. Log in to MariaDB as root:
    sudo mysql -u root -p
    
  2. Use the following sql statements to create database and user for your Magento2 Application:
    
    -- Create database
    CREATE DATABASE magento2;
    
    -- Create user for magento
    CREATE USER 'magentouser'@'localhost' IDENTIFIED BY 'your_password';
    
    -- Grant all privileges to the new user:
    GRANT ALL ON magento.* TO 'magentouser'@'localhost' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
    
    --Flush privileges and exit:
    FLUSH PRIVILEGES;
    EXIT;
    
    

Step 4: Install Elasticsearch

Elasticsearch is required for Magento’s search functionality.

  1. Install Java (Elasticsearch needs Java to run):
    sudo apt install openjdk-11-jdk
    
  2. Add Elasticsearch GPG key and repository:
    wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
    sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
    
  3. Install Elasticsearch:
    sudo apt update
    sudo apt install elasticsearch
    
  4. Start and enable Elasticsearch:
    sudo systemctl start elasticsearch
    sudo systemctl enable elasticsearch
    

Step 5: Download and Configure Magento

You can either downloads the Magento2 source code from official GitHub releases page. Another option is to to compose command line utility to setup it.

  1. Change to the web root directory:
    cd /var/www/html
    
  2. Download Magento using Composer:
    composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento2
    
  3. Change ownership of the Magento directory:
    sudo chown -R www-data:www-data /var/www/html/magento2
    sudo chmod -R 755 /var/www/html/magento2
    
  4. Change to the Magento directory:
    cd /var/www/html/magento2
    
  5. Set up Magento:
    bin/magento setup:install --base-url=http://your_domain_or_IP 
    		--db-host=localhost 
    		--db-name=magento 
    		--db-user=magentouser 
    		--db-password=your_password 
    		--admin-firstname=Admin 
    		--admin-lastname=User 
    		[email protected] 
    		--admin-user=admin 
    		--admin-password=admin_password123 
    		--language=en_US 
    		--currency=USD 
    		--timezone=America/Chicago 
    		--use-rewrites=1
    

Step 6: Setup Apache Virtual Host

We need to create an Apache virtual host configuration for Magento.

  1. Create a new virtual host file:
    sudo nano /etc/apache2/sites-available/magento2.conf
    
  2. Add the following configuration to the file:
    
    
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/magento2
        ServerName your_domain_or_IP
    
        
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        
    
        ErrorLog ${APACHE_LOG_DIR}/magento2_error.log
        CustomLog ${APACHE_LOG_DIR}/magento2_access.log combined
    
        
            AllowOverride All
            Options Indexes FollowSymLinks
            Require all granted
        
    
        
            AllowOverride All
            Options Indexes FollowSymLinks
            Require all granted
        
    
        
            AllowOverride All
            Options Indexes FollowSymLinks
            Require all granted
        
    
        
            AllowOverride All
            Options Indexes FollowSymLinks
            Require all granted
        
    
        
            AllowOverride All
            Options Indexes FollowSymLinks
            Require all granted
        
    
    
    
  3. Enable the new virtual host:
    sudo a2ensite magento2.conf
    
  4. Disable the default site:
    sudo a2dissite 000-default.conf
    
  5. Enable the Apache rewrite module:
    sudo a2enmod rewrite
    
  6. Restart Apache:
    sudo systemctl restart apache2
    

Step 7: Testing

Now, you should be able to access your Magento store in your web browser.

  1. Open your web browser.
  2. Navigate to http://your_domain_or_IP.


    If everything is set up correctly, you will see the Magento storefront.

Conclusion

Congratulations! You have successfully installed Magento 2 on your Ubuntu server. With Magento up and running, you now have a powerful platform to build and manage your online store. From adding products to configuring payment gateways and customizing your store’s appearance, Magento provides a vast array of tools to help you create a unique shopping experience for your customers.

Remember, setting up Magento is just the beginning. To make the most of this powerful e-commerce platform, you’ll want to explore its many features and extensions. Whether you need advanced marketing tools, detailed analytics, or enhanced security options, the Magento marketplace offers a wide range of add-ons to expand your store’s capabilities.

Thank you for following this tutorial. We hope it was helpful and easy to understand. Hope the online store helped you to reach your business at next level.