Magento is a open source e-commerce platform written in PHP. And it is highly popular ecommerce platform and owned and managed by Adobe Inc. The platform is flexible and has a large variety of features to build an online store.
In this tutorial we will guide you on how to install Magento 2.4.3 using the LAMP stack.
Let us start the installation.
1. Updating the system
sudo apt update -y && apt upgrade -y
2. Installing PHP and PHP extensions
The default PHP version of Ubuntu 20.04 is PHP 7.4. In this setup we will use PHP 7.4 since it is the required version for the 2.4.3 version. Let us proceed installing PHP and the extensions.
sudo apt install php libapache2-mod-php php-bcmath php-curl php-fpm php-gd php-intl php-mbstring php-mysql php-soap php-xml php-xsl php-zip php-cli
Let us modify some of the PHP values to meet the requirements.
Find the PHP configuration file.
root@techtutorguro:~# php --ini | grep "Loaded Configuration File"
Loaded Configuration File: /etc/php/7.4/cli/php.ini
Open the configuration file using your favorite editor.
nano /etc/php/7.4/cli/php.ini
Modify the configuration file with the values below.
memory_limit = 1G
upload_max_filesize = 128M
zlib.output_compression = on
max_execution_time = 18000
realpath_cache_size = 10M
realpath_cache_ttl = 7200
Then save the configuration file.
3. Database Installation and configuration
The database that we are going to use in this tutorial is MariaDB.
Let us proceed with the Database installation.
sudo apt install mariadb-server -y
Let us configure MariaDB server.
sudo mysql_secure_installation
Output:
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): Press Enter
You already have a root password set, so you can safely answer '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
Restart MariaDB service:
sudo systemctl restart mariadb
Let us create the database for our application.
First login to our MariaDB console.
sudo mysql -u root
Now create the Database name and Database user.
MariaDB [(none)]> create database magento2;
MariaDB [(none)]> grant all privileges on magento2.* to 'magento_user'@'localhost' identified by 'mypassword';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;
Please don’t forget to change the “mypassword” to your desired password.
4. Web server Installation and Configuration
Since this tutorial is on LAMP stack we will install Apache web server.
sudo apt install apache2
Let us start and enable Apache web server.
sudo systemctl start apache2
sudo systemctl enable apache2
Create a Virtual Host for our site.
nano /etc/apache2/sites-available/domain.com.conf
Add below content.
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/magento2/pub
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/">
AllowOverride all
</Directory>
</VirtualHost>
Please note to change the ServerName and ServerAlias to your own domain.
Let us enable the Virtual Host
a2ensite domain.com.conf
Then enable mod_rewrite module.
a2enmod rewrite
Restart Apache for the configuration to take effect.
systemctl restart apache2
5. Installing Elastic Search
Starting the 2.4 version, all installations must be configured to use Elasticsearch.
Before we proceed with the Elasticsearch installation we need to install the required dependencies.
sudo apt install apt-transport-https ca-certificates gnupg2
Import the GPG key.
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
Add the Elasticsearch repository.
sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
Let us now install Elasticsearch.
sudo apt update
sudo apt install elasticsearch
Then start and enable the service.
systemctl start elasticsearch
systemctl enable elasticsearch
6. Installing Composer
Composer is used to manage PHP packages.
Let us proceed in installing composer.
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php -d memory_limit=-1 composer-setup.php --install-dir=/usr/local/bin --filename=composer
NOTE: If you encounter “PHP Fatal error: Allowed memory size exhausted” in running composer better to set your PHP memory_limit value to -1 or unlimited.
7. Download and Install Magento 2.4.3
For most situation it is recommended to install it using the Marketplace by creating access key.
Let us create an access key.
- First Login to your Marketplace account
- Go to Name->My Profile
- Go to Marketplace->My Products->Access Keys
- Under Magento 2 tab click “Create A New Access Key“
- On the popup enter any name in textbox then click OK.
- You’re done and you will see the Access Keys generated.
Now Let us proceed with the Installation.
First run this command.
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.3 /var/www/magento2
After running the command above it will ask you to provide the username and password which is your Access keys.
Username: YOUR_PUBLIC_KEY
Password: YOUR_PRIVATE_KEY
After entering the credentials the installation will ask you if you want to store the credentials in ” /root/.config/composer/auth.json ” just select Y.
Do you want to store credentials for repo.magento.com in /root/.config/composer/auth.json ? [Yn] Y
Go to your Magento root directory.
cd /var/www/magento2
Let us now proceed with the installation.
Note: Please don’t forget to modify the values below based on your own set of values.
bin/magento setup:install \
--base-url=http://domain.com \
--db-host=localhost \
--db-name=magento2 \
--db-user=magento_user \
--db-password=mypassword \
--admin-firstname=admin \
--admin-lastname=admin \
[email protected] \
--admin-user=admin \
--admin-password=myadminpassword \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1
After the installation process you will see the admin link for your site.
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_5shsgt
NOTE: If your database password contains special characters(!@#$%^$*) you will encounter issue like this:
SQLSTATE[HY000] [1045] Access denied for user ‘user’@’localhost’
SOLUTIONS: add backslash before the special characters.
If your database password is something like this “myp@55w0rd“ your database password should be set below:
–db-password= myp\@55w0rd
8. Setup the Cron Jobs
Cron jobs is used to automate important operation. Let us setup the cron jobs.
sudo bin/magento cron:install
Before we access our instance we need to fix the permission.
sudo chown -R www-data:www-data /var/www/magento2
That’s it you can now access your Magento site but if you encounter issue logging in to your site you need to disable the Two factor authentication, to that run the following command.
sudo bin/magento module:disable Magento_TwoFactorAuth
Congratulations you have successfully installed Magento 2.4.3 you can now access your site at http://domain.com
Conclusion
You have learned how to install Magento 2.4.3 on Ubuntu 20.04 with the LAMP stack. The next obvious thing to do is secure your site by installing SSL certificate so we recommend you check our article on Let’s Encrypt.
If you have questions, feel free to leave a comment and we will try to answer it.
Thank you and hope you enjoy our tutorial ?
Your guide is incomplete. After you restart MariaDB you start firing sql commands but you are not logged in on the sql server.
Missing something like : sudo mysql -u root ?
Thank you for figuring that out, we already updated the article.
Everything was installed successfully but nothing appears on the site
Have you encountered issues on the installation. You need to check the webserver logs for further information.
This tutorial is not complete. When you get to this part:
cd /var/www/magento2 it gets to the directory fine, the next step says “Let us know proceed with the installation.” Did you guys leave a word out there? or did you misspell “Now”? This line is confusing. Then the next section shows this:
“bin/magento setup:install \” and all it does is go to a command prompt, this isn’t installing anything, so from here on the tutorial doesn’t work at all. Thank god this is being done on VMs, I can just trash the VM. Why does it stop working right there? Did you have a typo in there that broke everything?” I find ton of issues like this with tutorials for installs on linux, it’s like coping text from run magazine back in the 1980s to make a Commodore 64 program work, you type all this crap in and then it doesn’t work and since all you understood was “Put this here and type this here” you never understand why it didn’t work. This is why everyone uses stuff like windows. So what did I do wrong? Or is this just a bad tutorial?
We fixed the typos. The Magento 2 installation part need all the parameters to run the a successfully installation. So you don’t need to just run the first line you need to include the parameters.