Tuesday, December 19, 2023

Install Magento 2 On Ubuntu Using Nginx

 

In this tutorial we are going to Install and Setup the Initial project of Megento2.

Table of Contents:

Step 1: Install Nginx
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
Step 7: Install sample data for Magento2


Before starting the installation, you can check the system requirement for installing Magento2 Here.

What Is Magento ?

Magento is an open-source e-commerce platform that allows businesses to create and manage online stores. It provides a flexible and customizable framework for building robust and feature-rich e-commerce websites.

Here are some key features and characteristics of Magento:  E-commerce Functionality, Scalability and Flexibility, Multi-Store Capabilities, Themes and Customization, SEO-Friendly, Community and Marketplace

Magento is widely used by businesses of various sizes, from small and medium-sized enterprises to large corporations, to create and manage their online presence and drive e-commerce sales.


Step 1) Install Nginx

$ sudo apt update
$ sudo apt install nginx -y

Step 2) Install MySQL and Create Database for Magento2

$ sudo apt install mysql-server -y
$ sudo mysql

  • Change Root User Password


ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

  • Create User & Database for Magento2


CREATE USER 'mysql_user'@'localhost' IDENTIFIED BY '87gjksbhstJHRH675FSEgh46g';
CREATE DATABASE mysql_db;
GRANT ALL PRIVILEGES ON mysql_db.* TO 'mysql_user'@'localhost';
EXIT;


Step 3) Install PHP and required extensions

Note: Create php.sh name script file and paste all below content on it.



$ sh php.sh

  • These commands will automatically modify the specified values within the php.ini file.
  • sudo sed -i -e 's/memory_limit = 128M/memory_limit = 2G/g' /etc/php/8.2/fpm/php.ini
  • sudo sed -i -e 's/max_execution_time = 30/max_execution_time = 1800/g' /etc/php/8.2/fpm/php.ini
  • sudo sed -i -e 's/zlib.output_compression = Off/zlib.output_compression = On/g' /etc/php/8.2/fpm/php.ini
  • sudo sed -i -e 's/memory_limit = 128M/memory_limit = 2G/g' /etc/php/8.2/cli/php.ini
  • sudo sed -i -e 's/max_execution_time = 30/max_execution_time = 1800/g' /etc/php/8.2/cli/php.ini
  • sudo sed -i -e 's/zlib.output_compression = Off/zlib.output_compression = On/g' /etc/php/8.2/cli/php.ini

  • Restart PHP FPM.

$ sudo systemctl restart php8.2-fpm

Step 4) Install and configure Elasticsearch

Note: Make sure your Machine has morethen 4GB of RAM.

  • First, we will install Openjdk17 (Java) as Elasticsearch runs on Java:

$ sudo apt install openjdk-17-jdk
$ java -version

$ curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic.gpg
$ echo "deb [signed-by=/usr/share/keyrings/elastic.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
$ sudo apt update
$ sudo apt install elasticsearch


  • Configure Elasticsearch

Once the elasticsearch has been installed on your system, open the elasticsearch.yml configuration file.

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.

$ sudo vim /etc/elasticsearch/elasticsearch.yml

  • Uncomment Or Add below lines:

node.name: "My First Node"
cluster.name: my-application
network.host: 127.0.0.1
http.port: 9200

  • If your server only has 4GB of RAM, you must edit this setting.

$ sudo vim /etc/elasticsearch/jvm.options

  • Now change the Xms and Xmx values to 2GB

-Xms2g
-Xmx2g

  • 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
$ sudo systemctl status elasticsearch.service

Step 5) Install Composer

$ cd ~
$ curl -sS https://getcomposer.org/installer -o composer-setup.php
$ sudo php composer-setup.php --install-dir=/usr/bin --filename=composer
$ composer


Step 6) Download and Install Magento2

$ cd /var/www/html
$ sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.3 magento2

  • Create an account on Magento marketplace and Click Here to get private and public access key,

Create Access keys here. If you have access keys, you can use those.

Now enter Username  and password to start downloading

  • Username: Example public Key  2fc966a913d4e83b28041eeb3c3b72e5
  • Password: Example private key. 48e05400d17ca1bcb4e693825c45416e


  • Set file permissions

$ cd /var/www/html/magento2
$ 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:www-data .
$ sudo chmod u+x bin/magento


  • Install Magento 2

$ cd /var/www/html/magento2

  • You must use the command line to install Magento.

$ php bin/magento setup:install --base-url=http://your_domain_name.com --db-host=localhost --db-name=mysql_db --db-user=mysql_user --db-password=87gjksbhstJHRH675FSEgh46g --admin-firstname=Admin --admin-lastname=Admin --admin-email=admin@admin.com --admin-user=admin --admin-password=admin@123 --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

  1. --base-url : your domain, eg: http://your_domain_name.com. You can change this base URL later if you make mistake.
  2. --db-host : Database host, input localhost if you follow my tutorial
  3. --db-name : name of the database we created in step 2
  4. --db-user : name of the database user we created in sep 2
  5. --db-password : password of your mysql user

Now composer will start installing Magento 2.4. The process will take a while (approximately 5 minutes)


  • Change Domain & Document-Root of Virtual-host file.

Edit your virtual host file

$ sudo vim /etc/nginx/sites-available/magento



  • Activate the newly created virtual host by creating a symlink to it in the /etc/nginx/sites-enabled directory:


$ sudo ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled

$ sudo systemctl restart nginx



Note : If you are performing this task on your local machine then only use below one, otherwise jump to next blue point.

$ sudo vim  /etc/hosts

Add line at the last of this file

127.0.0.1 your_domain_name.com

  • Next, you will need to run these command to upgrade the database and deploy static view files



$ cd /var/www/html/magento2

$ 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

$
php bin/magento mod:dis Magento_AdminAdobeImsTwoFactorAuth Magento_TwoFactorAuth

  • Hit the URL http://your_domain_name.com in the browser. This is Magento 2 Home page


Admin Login

Your Admin Url – http://your_domain_name.com/admin/ 



No comments:

Post a Comment

testing