How to Install Magento 2 on Ubuntu 20.04 Server (Step by Step Tutorial)

How to Install Magento 2 on Ubuntu 20.04 Server. Magento is a free, open source and popular e commerce platform written in PHP. It is customizable, easy to use, and allows us to host a fully functional online store in minutes. Magento comes in two major editions, the community edition, and the commercial edition. The community edition is free, while the commercial edition is paid and designed for medium to large businesses.

Magento 2 Features

  • Catalog management.
  • Site management.
  • Product browsing.
  • Search engine optimization (SEO).
  • Marketing, promotion, and conversation tools.
  • Checkout, payment and shipping features.
  • Mobile commerce.
  • Manage customer accounts.
  • Customer accounts.
  • Order management.
  • Analytics and reporting.

This post will show you how to install Magento 2 on Ubuntu 20.04.

Install Magento 2 on Ubuntu 20.04 Server

Install LAMP Server Ubuntu

First, you will need to install the Apache Webserver, MySQL Database Server, PHP and other required packages to your server. You can install all of them by running the following command:

				
					apt-get install apache2 mysql-server php libapache2-mod-php php-common php-gmp php-curl php-soap php-bcmath php-intl php-mbstring php-xmlrpc php-mysql php-gd php-xml php-cli php-zip unzip curl -y
				
			

After installing all the packages, edit the php.ini file and change the default settings:

				
					nano /etc/php/7.4/apache2/php.ini
				
			

Change the following settings:

				
					file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 512M
upload_max_filesize = 128M
max_execution_time = 3600
				
			

Save and close the file then restart the Apache service to apply the changes:

				
					systemctl restart apache2
				
			

Magento Database

Magento uses MySQL as a database backend. So you will need to create a database and user for Magento. First, log in to MySQL with the following command:

				
					mysql
				
			

Once you are log in, create a database and user for Magento with the following command:

				
					mysql> CREATE DATABASE magento;
mysql> CREATE USER 'magento'@'localhost' IDENTIFIED BY 'password';
				
			

Next, grant all the privileges to the Magento with the following command:

				
					mysql> GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'localhost';
				
			

Next, flush the privileges and exit from the MySQL with the following command:

				
					mysql> flush privileges;
mysql> exit;
				
			

Download Magento 2

Before downloading Magento 2, you will need to install Composer on your system. You can install it by running the following command:

				
					curl -sS https://getcomposer.org/installer | php
				
			

You should see the following output:

				
					All settings correct for using Composer
Downloading...

Composer (version 2.2.4) successfully installed to: /root/composer.phar
Use it: php composer.phar
				
			

Next, move the Composer binary to the system location with the following command:

				
					mv composer.phar /usr/local/bin/composer
				
			

Next, change the directory to Apache web root and download the latest version of Magento:

				
					cd /var/www/html/
wget https://github.com/magento/magento2/archive/refs/tags/2.4.2.zip
				
			

Once Magento 2 is downloaded, unzip the downloaded file with the following command:

				
					unzip 2.4.2.zip
				
			

Next, move the extracted directory to the Apache web root directory:

				
					mv magento2-2.4.2 /var/www/html/magento2
				
			

Next, change the directory to magento2 and update the composer with the following command:

				
					cd magento2
composer update
composer install
				
			

Next, set proper permission and ownership to magento2 directory:

				
					chown -R www-data:www-data /var/www/html/magento2
chmod -R 755 /var/www/html/magento2
find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
chmod u+x bin/magento

				
			

Create an Apache Configuration File for Magento 2

In our guide how to Install Magento 2 on Ubuntu 20.04 next you will need to create an Apache Virtual Host configuration file for Magento 2. You can create it with the following command:

				
					nano /etc/apache2/sites-available/magento2.conf
				
			

Add the following lines:

				
					<VirtualHost *:80>
     ServerAdmin admin@example.com
      DocumentRoot /var/www/html/magento2/
     ServerName magento.example.com

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

     ErrorLog ${APACHE_LOG_DIR}/magento2_error.log
     CustomLog ${APACHE_LOG_DIR}/magento2_access.log combined

</VirtualHost>
				
			

Save and close the file then activate the Apache virtual host and rewrite module with the following command:

				
					a2ensite magento2.conf
a2enmod rewrite

				
			

Finally, restart the Apache service to apply the configuration changes:

				
					systemctl restart apache2
				
			

You can check the Apache status with the following command:

				
					systemctl status apache2
				
			

You should get the following output:

				
					● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-01-31 08:38:30 UTC; 5s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 16188 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 16208 (apache2)
      Tasks: 6 (limit: 2353)
     Memory: 13.4M
     CGroup: /system.slice/apache2.service
             ├─16208 /usr/sbin/apache2 -k start
             ├─16209 /usr/sbin/apache2 -k start
             ├─16210 /usr/sbin/apache2 -k start
             ├─16211 /usr/sbin/apache2 -k start
             ├─16212 /usr/sbin/apache2 -k start
             └─16213 /usr/sbin/apache2 -k start

				
			

Install Magento 2 CLI

Next, you will need to perform the Magento 2 installation from the command line interface because the Magento web installation wizard has been removed since version 2.3.7.

First change the directory to Magento2 and disable the Elasticsearch module:

				
					cd /var/www/html/magento2/
sudo -u www-data bin/magento module:disable {Magento_Elasticsearch,Magento_Elasticsearch6,Magento_Elasticsearch7}
				
			

Next, run the following command to start the installation:

				
					sudo -u www-data bin/magento setup:install --base-url=http://magento.example.com --db-host=localhost --db-name=magento --db-user=magento --db-password=password --admin-firstname=admin --admin-lastname=admin --admin-email=admin@example.com --admin-user=admin --admin-password=Secure@password123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1
				
			

Once the Magento 2 has been installed, you will get the following output:

				
					[Progress: 828 / 830]
Post installation file permissions check...
For security, remove write permissions from these directories: '/var/www/html/magento2/app/etc'
[Progress: 829 / 830]
Write installation date...
[Progress: 830 / 830]
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_194d63

				
			

Next, install the Cron job for Magento 2 with the following command:

				
					sudo -u www-data bin/magento cron:install
				
			

Access Magento Admin Interface

Now, open your web browser and access the Magento 2 admin interface using the URL https://magento.example.com/admin_194d63. You will be redirected to the Magento 2 login page:

Provide your admin username, password, and click on the Sign in button. You should see the Magento 2 dashboard on the following page:

Enable SSL on Magento

It is always recommended to enable the SSL on your website to secure the communication. First, you will need to install the Certbot client to install and manage the SSL. By default, the Certbot package is included in the Ubuntu default repository so you can install it with the following command:

				
					apt-get install python3-certbot-apache -y
				
			

Once the Certbot is installed, run the following command to secure your website with Let’s Encrypt SSL:

				
					certbot --apache -d magento.example.com
				
			

You will be asked to provide your email and accept the term of service as shown below:

				
					Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): hitjethva1981@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Plugins selected: Authenticator apache, Installer apache
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for magento.example.com
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/magento-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/magento-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/magento-le-ssl.conf

				
			

Next, select whether or not to redirect HTTP traffic to HTTPS as shown below:

				
					Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
				
			

Type 2 and hit Enter to install the Let’s Encrypt SSL for your website:

				
					Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/magento.conf to ssl vhost in /etc/apache2/sites-available/magento-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://magento.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=magento.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/magento.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/magento.example.com/privkey.pem
   Your cert will expire on 2022-4-29. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

				
			

You can now access your website sucurely using the URL https://magento.example.com.

How to Install Magento 2 on Ubuntu 20.04 Conclusion

Congratulations! you have successfully installed Magento 2 with Apache on Ubuntu 20.04. You can now use Magento 2 to host your own online store. For more information, visit the Magento 2 documentation.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

3 2 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x