This short post shows students and new users the steps to install and use Magento 2 on Ubuntu Linux with Apache HTTP web server. This post will also have a link to set up free Let’s Encrypt SSL certificates to protect your Magento websites and applications.

Magento is a free and open source e-commerce platform based on PHP and MySQL that is used by millions of small businesses to sell and manage their products online. If you want to create an online store, Magento might be the easiest way to do it, especially if you will need user support to manage and maintain the store.

Magento allows users to create a complete online store, including inventory management, product catalogs, shipping, billing, and many more.

This tutorial is based on Ubuntu Linux. We will install the Apache web server, the MariaDB database server and the PHP modules. We will also link to another post that will show you how to protect your Magento website using free SSL certificates from Let’s Encrypt.

For more information on Magento, see their Homepage

To start installing Magento on Ubuntu Linux, follow the steps below:

How to install Apache on Ubuntu Linux

As mentioned above, we will use the Apache web server to run Magento. Magento requires a web server to function, and Apache is one of the most popular open source web servers available today.

YOU CAN ALSO READ:   How to configure login with SSH key in Ubuntu Linux

To install Apache on Ubuntu, run the following commands:

sudo apt update
sudo apt install apache2

After installing Apache, the following commands can be used to stop, start and enable Apache services always start every time the server starts.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

To test if Apache is installed and working, open your web browser and look for the IP address or hostname of the server.

http: // localhost

If you see the above page in the browser, then Apache is working as expected.

How to install MariaDB on Ubuntu Linux

A database server is required for Magento to work. Magento stores its content in a database and MariaDB is probably the best database server available to run Magento.

MariaDB is fast, secure, and the default server for almost all Linux servers. To install MariaDB, run the following commands:

sudo apt install mariadb-server
sudo apt install mariadb-client

After installing MariaDB, the following commands can be used to stop, start and enable MariaDB services always start when the server starts.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Then run the following commands to protect the database server with a root password if you were not prompted to do so during installation.

sudo mysql_secure_installation

When prompted, use the guide below to respond:

If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): PRESS ENTER

Switch to unix_socket authentication [Y/n] n

Change the root password? [Y/n] n

Remove anonymous users? [Y/n] y

Disallow root login remotely? [Y/n] y

Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y

All done!

To verify and validate that MariaDB is installed and working, log into the database console using the following commands:

sudo mysql -u root -p

It should automatically log into the database server as we started the login request as root. Only root can login without password and only from server console.

If you see a screen similar to the one shown above, then the server installed successfully.

How to install PHP on Ubuntu Linux

As we also mentioned earlier, we are installing PHP on Ubuntu as Magento requires it. The PHP packages are added to the Ubuntu repositories. Repository versions may not be the latest. If you need to install the latest versions, you will need to add a third-party PPA repository.

In a third-party repository with the latest versions of PHP, run the following commands.

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

At the time of writing, the latest version of PHP 8.0.

sudo apt update

Then run the following commands to install PHP 8.0 and related modules.

sudo apt install php8.0 php8.0-common php8.0-mysql php8.0-gmp php8.0-curl php8.0-intl php8.0-mbstring php8.0-xmlrpc php8.0-gd php8.0-xml php8.0-cli php8.0-zip php8.0-soap php8.0-bcmath

Once PHP is installed, the following commands can be used to start, stop, and allow the PHP-FPM services to start automatically when the server starts.

sudo systemctl stop php8.0-fpm
sudo systemctl start php8.0-fpm
sudo systemctl enable php8.0-fpm

Next, you’ll want to change some PHP configuration settings that work great with Magento. Run the following commands to open the default PHP configuration file.

sudo nano /etc/php/8.0/fpm/php.ini

Then change the line settings to be something that lines up with the lines below. Save your changes and exit.

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

How to create a Magento database in Ubuntu

At this point, we are ready to create the Magento database. As mentioned above, Magento uses databases to store its content.

To create a database for Magento, run the following commands:

sudo mysql -u root -p

Then create a database called magentodb

CREATE DATABASE magentodb;

Next, create a database user named magentodbuser and set password

CREATE USER 'magentodbuser'@'localhost' IDENTIFIED BY 'new_password_here';

Then grant the user full access to the database.

GRANT ALL ON magentodb.* TO 'magentodbuser'@'localhost' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

How to download Magento 2

We are ready to download Magento and start configuring it. First, run the following commands to download the latest version of Magento from its repository.

To get the latest version of Magento, you may want to use the GitHub repository. Install Composer, Curl, and other dependencies to get started

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

After installing curl and Composer above, change to Apache root directory and download the Magento 2 packages from GitHub.

When prompted, enter your authentication keys. You Public key is your username; your private key is your password…. ( https://marketplace.magento.com

You will need to sign up for an account to create the above key.

Then run the command below to allow www-data user to own the Magento directory.

Run the following commands to create a new project called magento.

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

Copy and paste the authentication key. (You Public key is your username; your private key is your password)

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

cd /var/www/magento
sudo bin/magento setup:install --base-url-secure=https://example.com/ --db-host=localhost --db-name=magentodb --db-user=magentodbuser --db-password=db_user_password_here --dbadmin-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 of localhost. The administrator is administration; therefore: your storefront url is https://exmaple.com
  • The database server is on the same local host as the web server….
  • The name of the database is magento, and the magentouser and the password is db_user_password_here
  • Use server rewrite
  • The Magento manager has the following properties:
    • Name and surname are: Administator
    • The username is: administration
  • and the password is admin123
  • The email address is: admin@example.com
  • The default language is: (American English)
  • The default currency is: American dollars
  • The default time zone is: Central United States (America / Chicago)

After that, run the following commands to set the correct permissions for Magento 2 to work.

sudo chown -R www-data:www-data /var/www/magento/
sudo chmod -R 755 /var/www/magento/

How to configure Apache for Magento

We have downloaded the Magento content into a new folder that we call Magento. Now, let’s configure Apache to create a new server block to use with our Magento website. You can create so many server blocks with Apache.

To do that, run the following commands to create a new configuration file called magento.conf at / etc / apache2 / sites-available / directory to host our Magento server block.

sudo nano /etc/apache2/sites-available/magento.conf

In the archive, copy and paste the content below in the archive and save it.

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.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.

After saving the above file, run the following commands to enable the new file that contains our Magento server block. Restart Apache after that.

sudo a2ensite magento.conf
sudo systemctl restart apache2

At this stage, Magento is ready and can be started by going to the server’s IP or hostname.

http://localhost

However, we want to make sure our server is protected with free Let’s Encrypt SSL certificates. So, continue below to learn how to generate Let’s Encrypt SSL certificate for websites.

How to configure Let’s Encrypt for Magento

We have written an excellent post on how to generate and manage Let’s Encrypt SSL certificates for Apache web server. You can use that post to apply here for your Magento website.

To read the post on how to generate Let’s Encrypt SSL certificates for the website, click the link below:

How to configure Let’s Encrypt SSL / TLS in Ubuntu with Apache support

If you were successful in generating a Let’s Encrypt SSL certificate, you need to reopen the server block for our Magento website by running the commands below.

sudo nano /etc/apache2/sites-available/magento.conf

The new Magento server block configurations should look similar to the following line. Take notes on the highlighted lines.

  • The first server block listens on port 80. It contains a 301 redirect to redirect HTTP to HTTPS.
  • The second server block listens on port 443. It contains a 301 redirect to redirect www to a domain other than www.
<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com

  Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/magento

  Protocols h2 http:/1.1

  <If "%{HTTP_HOST} == 'www.example.com'">
    Redirect permanent / https://example.com/
  </If>
  
  ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
  CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"
  
  SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384

  SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
  SSLCompression off
  SSLUseStapling on

  Header always set Strict-Transport-Security "max-age=63072000"

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

Save the file above, then restart Apache and PHP using the commands below.

sudo systemctl restart apache2

Finally, if everything went according to plan, you should be able to start the Magento setup wizard by navigating to the server’s hostname or IP address via HTTPS.

https://example.com/

Next, open your browser and search for the domain name of the server. You should see the Magento home page.

Now you can log in as an administrator and start customizing and building your store.

How to update Magento 2

First stop the web server.

sudo systemctl stop apache2

In the future, when you want to upgrade to a new released version, just run the following commands to upgrade …

cd /var/www/magento
sudo bin/magento maintenance:enable
sudo composer require magento/product-community-edition 2.2.5 --no-update
sudo composer update
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento indexer:reindex
sudo php bin/magento maintenance:disable

You may need to rerun the to update the Apache directory permissions.

Conclusion:

In this tutorial we have seen how to install Magento 2 on Ubuntu Linux with a link to configure Let’s Encrypt. If you find any errors above or have something to add, please use the comment form below.

Write A Comment