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.
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:
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
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:
ServerAdmin admin@example.com
DocumentRoot /var/www/html/magento2/
ServerName magento.example.com
Options FollowSymlinks
AllowOverride All
Require all granted
ErrorLog ${APACHE_LOG_DIR}/magento2_error.log
CustomLog ${APACHE_LOG_DIR}/magento2_access.log combined
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:
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:
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:
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.
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.73votes
Article Rating
Subscribe
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
Login and comment with
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.