Introduction
Magento is an open-source E-commerce platform written in PHP. Magento uses several PHP frameworks (such as Symfony and Laminas), MySQL database, and parts of the Zend Framework. It is a useful software for online business and is currently the world’s largest e-commerce platform. The platform is flexible and has a wide range of features to build an online store.
Magento is originally developed by Roy Rubin and Yoav Kutner at Varian Inc. Magento was first released on March 31, 2007. Magento was acquired by Adobe Inc in May 2018.
In this tutorial, we will discuss how to install Magento 2.4.5 Community Edition on Ubuntu 22.04 Server. Magento 2.x installation and configuration is a bit tricky, you can install it easily by following this tutorial step-by-step.
Prerequisites
- Fresh VPS with Ubuntu 22.04 OS Installed
- VPS with at least 2 GB of RAM
- Root SSH access
Step 1 — Install Apache2
Connect to the Ubuntu 22.04 Server through SSH and update your server’s package using the following command
sudo apt update
Now, install apache2
sudo apt install apache2
Note: After you run the install command, it will ask to continue yes/no. Type “y” then enter to continue.
After the installation is complete, verify the installed Apache version and status using the following command
sudo apache2ctl -v
systemctl status apache2
Also, you can open your domain or IP in browser, you see the Apache default page.
Now, enable auto start-up for Apache by running the following command
systemctl is-enabled apache2
Now, We need to setup virtual hosts for the domain
Go to /var/www/ and create a directory for the domain “teckassist.com”.
Note: Change the domain name with your domain.
cd /var/www/
sudo mkdir -p /var/www/teckassist.com/
Change the ownership of “teckassist.com” directory by running the following command.
sudo chown -R www-data:www-data /var/www/teckassist.com
Now, create a test webpage “index.html”
sudo nano /var/www/teckassist.com/index.html
Now, create an apache virtualhost by running the following command
sudo nano /etc/apache2/sites-available/teckassist.com.conf
Add the following code into the conf. file
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName teckassist.com ServerAlias www.teckassist.com DocumentRoot /var/www/teckassist.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Note: Change the inputs in conf. file such as “ServerName”, “ServerAlias”, and “DocumentRoot”
Now, enable the virtualhost by runing the following command
sudo a2ensite teckassist.com.conf
After that, restart the apache to reflect the changes
sudo systemctl restart apache2
Step 2 — Install MySQL
Install MySQL by running the following command
sudo apt install mysql-server
Note: After you run the install command, It will ask to continue yes/no. Type “y” then enter to continue.
After the installation is complete, verify the installed MySQL version using the following command
mysql -V
Now, secure MySQL installation by running the following command
sudo mysql_secure_installation
Step 3 — Create a Magento2 Database
Connect MySQL and create the database and user by running the following command
mysql -u root -p CREATE DATABASE magento2; CREATE USER 'magento2'@'localhost' IDENTIFIED BY 'secure_password';
Note: Don’t forgot to change ‘secure_password‘ with your password.
ALTER USER 'magento2'@'localhost' IDENTIFIED WITH mysql_native_password BY 'secure_password'; GRANT ALL PRIVILEGES ON *.* TO 'magento2'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT
Step 4 — Install PHP 8.1 and necessary PHP extensions
Now, install PHP 8.1 with packages by running the following command
sudo apt install php8.1 php8.1-bcmath php8.1-intl php8.1-soap php8.1-zip php8.1-gd php8.1-curl php8.1-cli php8.1-xml php8.1-xmlrpc php8.1-gmp php8.1-mbstring php8.1-common
After that, verify the PHP version by running the following command
php -v
Now, Enable the Apache rewrite module by running the following command
sudo a2enmod rewrite
Then, Reload the apache2 configuration
sudo systemctl reload apache2
Now, change the PHP settings for magento2
First, run the following command to find the PHP configuration path
php -i | grep "Configuration File"
Now, Edit the PHP configuration file
sudo nano /path/to/phpfile/php.ini
change the limits as follows:
Save the configuration file and reload the apache2
sudo systemctl reload apache2
Step 5 — Install Elasticsearch
First, we need to install Openjdk17 (Java) as Elasticsearch runs on Java
sudo apt install openjdk-17-jdk
After that, import the GPG key for Elasticsearch packages by running the following command
sudo apt install curl sudo curl -sSfL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --no-default-keyring --keyring=gnupg-ring:/etc/apt/trusted.gpg.d/magento.gpg --import sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list' sudo chmod 666 /etc/apt/trusted.gpg.d/magento.gpg
After that, update the system by running the following command
sudo apt update
Install Elasticsearch packages
sudo apt install elasticsearch
Now, start and enable the elasticsearch by running the following commands
sudo systemctl daemon-reload sudo systemctl enable elasticsearch.service sudo systemctl start elasticsearch.service
Now, verify elasticsearch by running the following command
curl -X GET "localhost:9200"
Step 6 — Install Composer
Install composer by running the following command
sudo apt install composer
Verify the composer by running the following command
composer -V
Step 7 — Download Magento2
We have setup the magento2 environment completely, now we require access key to download magento
To generate and get the access keys, create an account on Magento marketplace and go to access key page https://marketplace.magento.com/customer/accessKeys/ to get private and public access key,
My profile > Marketplace > My products > Access Keys
Username: Your Public Key
Password: Your Private Key
After generating the access keys, download the magento by running following commands
Go to download directory
cd /var/www/teckassist.com sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.5 magento2
After running the above command, it will ask for your username and password, enter the access keys
Username: Your Public Key
Password: Your Private Key
The download process may take some time, wait for few minutes to complete the process.
Step 8 — Install Magento2
cd /var/www/teckassist.com/magento2
Run the following command
Note: Change the following inputs such as base-url, database inputs, admin email address and admin password, before running the following command.
bin/magento setup:install \ --base-url=http://domain.com \ --db-host=localhost \ --db-name=magento2 \ --db-user=magento2 \ --db-password=secure_password \ --admin-firstname=Admin \ --admin-lastname=User \ --admin-email=admin@domain.com \ --admin-user=admin \ --admin-password=admin123 \ --language=en_US \ --currency=USD \ --timezone=America/Chicago \ --use-rewrites=1
The installation process may take some time (around 20 minutes), wait for some time to complete the process.
After the installation process completes, we see the following output:
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_123xyz
Now, change the ownership of the /var/www/teckassist.com/magento2 directory to the www-data user:
chown -R www-data: /var/www/teckassist.com/magento2
Now, change the DocumentRoot in apache virtual host conf. file
Change the DocumentRoot from “/var/www/teckassist.com/magento2” to “/var/www/teckassist.com/magento2/pub”
sudo nano /etc/apache2/sites-available/teckassist.com.conf
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName teckassist.com ServerAlias www.teckassist.com DocumentRoot /var/www/teckassist.com/magento2/pub ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory "/var/www/teckassist.com"> AllowOverride all </Directory> </VirtualHost>
Now, restart the apache to reflect the changes
sudo systemctl restart apache2
Now, upgrade the database and deploy static view files by running the following command
php bin/magento indexer:reindex && php bin/magento se:up && php bin/magento se:s:d -f && php bin/magento c:f && php bin/magento module:disable Magento_TwoFactorAuth
That’s it, Visit to the url “yourdomain.com/magento2”. You will see the default magento2 page.
And, for admin you can visit url http://yourdomain.com/magento2/admin_123abc/