Magento 2.4.2 has been delivered with a ton of safety, GraphQL enhancements simultaneously with hundreds of bug fixes. You can follow the post “Prepare Magento 2 Development Environment in ubuntu” to have the option to introduce it effectively.
To begin this establishment, you ought to set yourself up with a VPS
running Ubuntu working framework.
Before going with this process, we need to check Magento’s system requirements every version has its own requirements but we always recommend using the latest stable version of Magento.
Here we are going to prepare the Environment for Magento version 2.4.3 and its requirements are:
- Composer 2
- ElasticSearch 7.9
- MySQL 8.0
- PHP 7.4
- Apache 2.4
- Redis 6.0
Table of Contents
Install Apache2
First, we will install the Apache webserver. Connect to your server using ssh protocol with root access and run this command:
Apache is available in Ubuntu’s default software repositories. For this reason, we’ll start by updating the local package index for the latest changes.
sudo apt update
Then, let’s install the apache2 package.
sudo apt install apache2
The system will ask to continue yes/no. Input “y” then enter to proceed.
Once installed, you can check the installed Apache version with the following command:
sudo apache2ctl -v
To verify if apache2 was install properly, in your browser, enter your domain or ip address or 127.0.0.1. If the result is apache default page then you’re good.
To enable auto startup for apache, use this command:
systemctl is-enabled apache2
Apache will be automatically started everytime you reboot your server.
Install MySQL:
Install MySQL using the apt command:
sudo apt install mysql-server
When prompted, type Y to confirm installation and then press ENTER.
When the installation is complete, let’s check the MySQL version.
mysql -V
When the installation is complete, run the following command to secure your MySQL. This command will remove some insecure default settings and block access to your database system.
sudo mysql_secure_installation
When prompted, press Y to set up validate the password.
Next, select a level of password validation. Enter your root password. The server will show the password strength for the root password you just entered and the server will ask if you want to continue with that password. If you accept, press Y.
For the rest of the questions, press Y and press ENTER key at each prompt.
When finished, Test login to MySQL with root user by the command:
sudo mysql
Configure Password Access for the MySQL Root Account
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_secure_password';
Note: Replace ‘your_secure_password‘ with your password.
Verify MySQL.user table with the command:
SELECT user,authentication_string,plugin,host FROM mysql.user;
exit
Create a new MySQL user for Magento 2
Login with root user.
mysql -u root -p
CREATE USER 'magento2'@'localhost' IDENTIFIED BY 'your_secure_password';
Note: Replace ‘your_secure_password‘ with your password.
ALTER USER 'magento2'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_secure_password';
Grant all privileges to magento2 users.
GRANT ALL PRIVILEGES ON *.* TO 'magento2'@'localhost' WITH GRANT OPTION;
exit
Create Magento 2 database
mysql -u magento2 -p
CREATE DATABASE magento2;
exit
Install PHP and required extensions
In this step, we will install PHP. Magento 2.4 require PHP 7.4, so we will install php 7.4 in this tutorial.
Update your APT repositories.
sudo apt update
Install PHP 7.4 and packages with command:
sudo apt install php7.4 libapache2-mod-php php-mysql
Next, verify your PHP version:
php -v
Enable the Apache rewrite module.
sudo a2enmod rewrite
Install PHP modules for Magento 2.4.x.
sudo apt install php7.4-bcmath php7.4-intl php7.4-soap php7.4-zip php7.4-gd php7.4-json php7.4-curl php7.4-cli php7.4-xml php7.4-xmlrpc php7.4-gmp php7.4-common php7.4-mbstring
Reload Apache for the changes to take effect.
sudo systemctl reload apache2
Configure php settings
php -i | grep "Configuration File"
From this command, you will get path to the php.ini file. Open this file to modify some settings:
sudo vi <Loaded Configuration File>
In php.ini file, search and change the following values as below:
max_execution_time=18000
max_input_time=1800
memory_limit=4G
Save the file. Reload Apache2.
sudo systemctl reload apache2
These values will keep the installation go properly without interruption.
Install and configure Elasticsearch
From Magento 2.4, Elasticsearch has become compulsory component. From Magento 2.4, catalog search will no longer use mysql, instead the system will use Elasticsearch by default.
So let’s install and configure it.
Install Elasticsearch
First, we will install Openjdk16 (Java) as Elasticsearch runs on Java:
sudo apt install openjdk-16-jdk
Next, verify if the java was installed properly and check its version with this syntax
java -version
The first step is to import the GPG key for Elasticsearch packages using the following command:
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
Then you need to add Elasticsearch repository to the system using the following command:
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
sudo apt update
sudo apt install elasticsearch
Finally, you can use the following commands Start and enable the Elasticsearch service:
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
Configure Elasticsearch
Let’s start editing the main elasticsearch.yml configuration file. Open it using vim or your preferred text editor:
sudo vi /etc/elasticsearch/elasticsearch.yml
Search for the line that contains network.host, uncomment it, and change the value to 127.0.0.1
Set the network host to 127.0.0.1 to listen on all interfaces and make it available publicly,
network.host: 127.0.0.1
http.port: 9200
Save and exit the file. Now start Elasticsearch for the first time:
sudo systemctl daemon-reload
sudo systemctl start elasticsearch.service
Testing Elasticsearch
By now, Elasticsearch should be running on port 9200. You can test this using curl, the command-line tool for client-side URL transfers. To test the service, make a GET request like this:
curl -X GET 'http://localhost:9200'
Output:
ubuntu@ip-172-31-19-102:~$ curl -X GET 'http://localhost:9200'
{
"name" : "ip-172-31-19-102",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "1ioNdJkfSLqzKn0PO9mQvA",
"version" : {
"number" : "7.16.0",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "6fc81662312141fe7691d7c1c91b8658ac17aa0d",
"build_date" : "2021-12-02T15:46:35.697268109Z",
"build_snapshot" : false,
"lucene_version" : "8.10.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
Now we have finished installing Elasticsearch.
Install Composer
we will install Magento 2.4 using composer. First, run this command to download composer installer.
Move back to the root directory.
cd ~
Downloading and installing composer.
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/bin --filename=composer
Let’s check the composer version.
composer
Our composer version is 2.1.6. Now we are ready to download Magento 2.4 using composer.
Download and Install Magento2
Download Magento2
Go to html folder by command:
cd /var/www/html
Create a new Composer project using the Magento Open Source or Magento Commerce metapackage.
Magento Open Source
In this post we will work with Magento open source version. and the Command for this is:
sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition
Example:
sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento2
Download Magento 2 with the specified version.
sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=
Example for version 2.4.2:
sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.2-p1 magento2
On Magento marketplace and go to https://marketplace.magento.com/customer/accessKeys/ to get private and public access key, and if you don’t have an account you need to create one to be able to have the keys.
Create Access keys here. If you have access keys, you can use those.
Now enter Username and password to start downloading
- Username: Your public Key xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- Password: Your private key xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Install Magento 2
Everything is ready, now we will run the final command to install Magento 2.4
Go to Magento 2 install directory.
cd /var/www/html/
Example:
cd /var/www/html/magento2
You must use the command line to install Magento.
sudo php bin/magento setup:install --base-url= --db-host=localhost --db-name=magento2 --db-user=magento2 --db-password= --admin-firstname=Admin --admin-lastname=Admin [email protected] --admin-user=admin --admin-password= --language=en_US --currency=USD --timezone=America/Chicago --backend-frontname=admin --use-rewrites=1 --search-engine=elasticsearch7 --elasticsearch-host=localhost --elasticsearch-port=9200
Replace the following information with your information
–base-url : your domain, eg: sonal.magento.com. You can change this base URL later if you make mistake.
–db-host : Database host, input localhost if you follow my tutorial
–db-name : name of the database we created in step 2
–db-user : name of the database user we created in step 2
–db-password : password of your mysql user
After thisis done!.
Congratulation, Magento 2.4 was successfully installed to your ubuntu server.
Configure Apache to launch our Magento portal
Edit your virtual host file
sudo vi /etc/apache2/sites-available/000-default.conf
Add the path to your Magento pub/ directory to the DocumentRoot directive:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/magento2/pub
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/html">
AllowOverride all
</Directory>
</VirtualHost>
Run Magento Commands
cd /var/www/html/magento2
Set the permissions for magento file systems
sudo chmod -R 777 var pub/static generated generated/ var/
Next, you will need to run these command to upgrade the database and deploy static view files
sudo php bin/magento indexer:reindex
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento setup:static-content:deploy -f
sudo php bin/magento cache:flush
Now navigate to your http://{{domain}}/, http://{{IP Address}}/ or http://localhost/ to see the Magento 2 homepage.
Installing and Configuring Redis
Install Redis
In order to get the latest version of Redis, we will use apt to install it from the official Ubuntu repositories.
First, update your local apt package cache if you haven’t done so recently:
sudo apt update
Then, install Redis by typing:
sudo apt install redis-server
This will download and install Redis and its dependencies. Following this, there is one important configuration change to make in the Redis configuration file, which was generated automatically during the installation.
Open this file with your preferred text editor:
sudo vi /etc/redis/redis.conf
Inside the file, find the supervised directive. This directive allows you to declare an init system to manage Redis as a service, providing you with more control over its operation. The supervised directive is set to no by default. Since you are running Ubuntu, which uses the systemd init system, change this to systemd:
. . .
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no – no supervision interaction
# supervised upstart – signal upstart by putting Redis into SIGSTOP mode
# supervised systemd – signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto – detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal “process is ready.”
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd. . .
That’s the only change you need to make to the Redis configuration file at this point, so save and close it when you are finished. Then, restart the Redis service to reflect the changes you made to the configuration file:
sudo systemctl restart redis.service
With that, you’ve installed and configured Redis and it’s running on your machine. Before you begin using it, though, it’s prudent to first check whether Redis is functioning correctly.
checking that the Redis service is running:
sudo systemctl status redis
Configure Magento to use Redis
Use Redis for default cache
Magento provides command line options to configure the Redis page and default caching. Although you can configure caching by editing the app/etc/env.php file, using the command line is the recommended method, especially for initial configurations. The command line provides validation, ensuring the configuration is syntactically correct.
Run the setup:config:set command and specify parameters that specific to Redis default caching.
bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-=...
where
–cache-backend=redis enables the Redis default caching. If this feature has already been enabled, omit this parameter.
–cache-backend-redis-= is a list of key/value pairs that configure the default caching.
Example command
The following example enables Redis default caching, sets the host to 127.0.0.1, and assigns the database number to 0. Redis uses default values for all other parameters.
bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db=1
Use Redis for session storage
Magento now provides command-line options to configure Redis session storage. In previous releases, you edited the app/etc/env.php file. The command line provides validation and is the recommended configuration method, but you can still edit the env.php file.
Run the setup:config:set command and specify Redis-specific parameters.
bin/magento setup:config:set --session-save=redis --session-save-redis-=...
where
–session-save=redis enables Redis session storage. If this feature has already been enabled, omit this parameter.
–session-save-redis-= is a list of parameter/value pairs that configure session storage.
Example command:
The following example sets Redis as the session data store, sets the host to 127.0.0.1, sets the log level to 4, and sets the database number to 2. All other parameters are set to the default value.
bin/magento setup:config:set --session-save=redis --session-save-redis-host=127.0.0.1 --session-save-redis-log-level=4 --session-save-redis-db=2
Khatab Khatir
As an accomplished and experienced lead developer, I have implemented a number of high volume projects from the ground up using a wide range of technologies. I worked primarily with the Magento eCommerce platform, managing the complexities involved in building eCommerce solutions tailored to a client’s specific needs. I have been writing extensions to take advantage of Magento’s higher functionality, and I was lucky to work with a number of mobile app developers on apps for the Magento website.