Table of Contents
Introducing Magento 2 is never a simple errand. Since the absolute first delivery, there are numerous issues clients may experience when introducing Magento 2.
With the most recent arrival of Magento 2.4.7, things have gotten more earnestly as Magento 2 currently.
To make a Magento 2 establishment work appropriately in the Production climate, clients are prescribed to introduce Magento 2 in localhost first. At that point move the neighborhood establishment to a Magento 2 facilitating and update storeβs Baseurl, and reindex Magento 2.
In this instructional exercise, we will experience the way toward introducing Magento 2 (with Elasticsearch) in Ubuntu.
If you are also want to install Magento2 on Windows. Click Here.
Contents-
- Step 1: Install Apache2
- Step 2: Install MySQL and Create Database for Magento2
- Step 3: Install PHP and required extensions
- Step 4: Install and configure Elasticsearch
- Step 5: Install Composer
- Step 6: Download and Install Magento2
- Wrapping up
Before starting the installation, you can check theΒ system requirement for installing Magento2 here.
Step 1: Install Apache2
First, we will install Apache web server. 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, lets 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 => everything is fine
To enable auto startup for apache, use this command:
systemctl is-enabled apache2
Step 2: Install MySQL and Create Database for Magento2
Install MySQL using apt command:
sudo apt install mysql-server
When prompted, typingΒ Y
Β to confirm installation and then pressΒ ENTER
.
When the installation is complete, lets check the mysql version
Configure Password Access for the MySQL Root Account
Login with root user.
sudo mysql
Note: Replace βyour_secure_password β with your password. |
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_secure_password';
exit
Create new MySQL user for Magento 2
Login with root user.
mysql -u root -p
SELECT user,authentication_string,plugin,host FROM mysql.user;
Note: Replace βyour_secure_password β with your password. |
CREATE USER 'magento2'@'localhost' IDENTIFIED BY 'your_secure_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
Lets create the database for our magento2: –
mysql -u magento2 -p
Then create
CREATE DATABASE magento2;
exit
Step 3: 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 8.1 and packages with command:
sudo add-apt-repository ppa:ondrej/php && sudo apt update sudo apt install php8.3 libapache2-mod-php php-mysql
Next, verify your PHP version:
php -v
sudo nano /etc/apache2/mods-enabled/dir.conf
Modify the index.php file order to the top listed in the DirectoryIndex.
The dir.conf file after modifying will look like this:
Install and enableΒ mbstring
Β extension.
sudo apt install php8.3-mbstring
sudo phpenmod mbstring
Enable the Apache rewrite module.
sudo a2enmod rewrite
Install PHP modules for Magento 2.4.7.
sudo apt install php8.3-bcmath php8.3-intl php8.3-soap php8.3-zip php8.3-gd php8.3-curl php8.3-cli php8.3-xml php8.3-xmlrpc php8.3-gmp php8.3-common
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 nano <path_of_php.ini_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.
Step 4: 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 Openjdk17 (Java) as Elasticsearch runs on Java:
sudo apt install openjdk-17-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 apt install curl
Once done, run below 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
After completing the above steps, you must firstΒ update the cacheΒ using the following commands. ThenΒ install Elasticsearch packagesΒ on the system:
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
sudo systemctl start elasticsearch.service
Configure Elasticsearch
Once the elasticsearch has been installed on your system, open the elasticsearch.yml configuration file.
Now that you have installed Elasticsearch and its Java dependency, it is time to configure Elasticsearch.
The Elasticsearch configuration files are in theΒ /etc/elasticsearch
directory. The ones weβll review and edit are:
elasticsearch.yml
Β β Configures the Elasticsearch server settings. This is where most options are stored, which is why we are mostly interested in this file.jvm.options
β Provides configuration for the JVM such as memory settings.
The first variables to customize on any Elasticsearch server are node.name
Β andΒ cluster.name
Β inΒ elasticsearch.yml
. Letβs do that now.
As their names suggest,Β node.name
Β specifies the name of the server (node) and the cluster to which the latter is associated. If you donβt customize these variables, aΒ node.name
Β will be assigned automatically in respect to the server hostname. TheΒ cluster.name
Β will be automatically set to the name of the default cluster.
TheΒ cluster.name
Β value is used by the auto-discovery feature of Elasticsearch to automatically discover and associate Elasticsearch nodes to a cluster. Thus, if you donβt change the default value, you might have unwanted nodes, found on the same network, in your cluster.
Letβs start editing the mainΒ elasticsearch.yml
configuration file. Open it usingΒ nano
Β or your preferred text editor:
sudo nano /etc/elasticsearch/elasticsearch.yml
Remove theΒ #
Β character at the beginning of the lines forΒ node.name
Β andΒ cluster.name
Β to uncomment them, and then change their values. Your first configuration changes in theΒ /etc/elasticsearch/elasticsearch.yml
Β file will look like this:
...
Now we have finished installing Elasticsearch.
Now let’s install next component – Composer.
Step 5: Install Composer
In this tutorial, we will install Magento 2.4 using composer. First, run this command to download composer installer.
Move back to the root directory.
cd ~
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.8.1. Now we are ready to download Magento 2.4.7 using composer.
Step 6: Download and Install Magento 2.4.7
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.
Command for this is :-
sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.7-p2 magento2
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Β 2fc966a913d4e83b28041eeb3c3b72e5
- Password: Your private key. 48e05400d17ca1bcb4e693825c45416e
You will see this screen if everything is fine. Now just wait some minutes, composer is download Magento 2 to your server
Wait the process to complete.
Set file permissions
cd /var/www/html/<magento install directory>
sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} + sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} + sudo chown -R user_name:www-data . sudo chmod u+x bin/magento
sudo chown -R user_name:www-data .
Β => user_name is the user with root accessInstall Magento 2
Everything is ready, now we will run the final command to install Magento 2.4.7
First, change the current directory to your website folderΒ (this is also where you downloaded Magento 2 data in step 3)
Go to Magento 2 install directory.
cd /var/www/html/<magento install directory>
You must use the command line to install Magento.
php bin/magento setup:installΒ
--base-url=<your-domain>Β
--db-host=localhostΒ
--db-name=magento2Β
--db-user=magento2Β
--db-password=<your-db-password-of-magento2-user>Β
--admin-firstname=AdminΒ
--admin-lastname=AdminΒ
--admin-email=admin@admin.comΒ
--admin-user=adminΒ
--admin-password=<your-admin-password>Β
--language=en_USΒ
--currency=USDΒ
--timezone=America/ChicagoΒ
--backend-frontname=adminΒ
--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
Now composer will start installing Magento 2.4.7. The process will take a while (approximately 20 minutes)
Wait until the installation is successful.
When everything is done you will see this
Congratulation, Magento 2.4 was successfully installed to your ubuntu server.
Change DocumentRoot To Pub
If you append a directory name to your serverβs hostname or IP address to create the base URL when you installed Magento (for example http://<your-ip>/magento2 or http://<your-sever-hostname>/magento2), youβll need to remove it.
Edit your virtual host file
sudo nano /etc/apache2/sites-available/sonal.magento.com
.conf
Add the path to your MagentoΒ pub/
Β directory to theΒ DocumentRoot
directive:
Modify file as shown below:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/magento2/pub
Β Β Β Β Β ServerName
Β sonal.magento.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/html">
AllowOverride all
</Directory>
</VirtualHost>
Restart Apache for the changes to take effect:
sudo systemctl restart apache2
If you are installing Magento locally, you can add domain.com.
Β Then you have to updateΒ hosts
Β file atΒ /etc/hosts
Β with
sudo nano /etc/hosts
127.0.0.1 sonal.magento.com
127.0.0.1 sonal.magento.com
sudo a2ensite sonal.magento.com
.conf
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
Hit the URL http://sonal.magento.com in the browser. This is Magento 2 Home page
Admin Login
Your Admin Url βΒ http://sonal.magento.com/admin/Β
Step 7: Install sample data for Magento 2
Now we will deploy sample data for our new Magento 2.4.4 website on localhost. Sample data will fill your website with some products, categories and images so your website will look like this:
To install the sample data, run the below command
php bin/magento sampledata:deploy && php bin/magento indexer:reindex && php bin/magento se:up && php bin/magento se:s:d -f && php bin/magento c:f
Well done! refresh your website and enjoy your new Magento 2.4 installation with sample data
Wrapping up
I have tried my best to give you the step-by-step tutorial so that it will be helpful for users who are having trouble installing Magento 2.4. If you face any problems during installation process, please drop a comment below (better with image describing your problem). I will be happy to help!