How to install MySQL in Ubuntu.

About MySQL

MySQL is open-source database management system. This tutorial will go over how to install MySQL version 8.0 on an Ubuntu 20.04 server. By completing it, you will have a working relational database that you can use to build your next website or application.

Installing MySQL

On Ubuntu, you can install MySQL using the APT package repository. At the time of this writing, the version of MySQL available in the default Ubuntu repository is version 8.0.27.

To install it, update the package index on your server if you’ve not done so recently:

sudo apt update

Then install the mysql-server package:

sudo apt install mysql-server

Ensure that the server is running using the systemctl start command:

sudo systemctl start mysql.service

These commands will install and start MySQL, but will not prompt you to set a password or make any other configuration changes. Because this leaves your installation of MySQL insecure, we will address this next.

Configuring MySQL

First, open up the MySQL prompt:

sudo mysql

Then run the following ALTER USER command to change the root user’s authentication method to one that uses a password. The following example changes the authentication method to mysql_native_password:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

After making this change, exit the MySQL prompt:

mysql> exit

That’s all for installing and configure MySQL in ubuntu.

How to install elasticsearch in ubuntu

About Elasticsearch.

Elasticsearch is the distributed search and analytics engine at the heart of the Elastic Stack. Logstash and Beats facilitate collecting, aggregating, and enriching your data and storing it in Elasticsearch. Kibana enables you to interactively explore, visualize, and share insights into your data and manage and monitor the stack. Elasticsearch is where the indexing, search, and analysis magic happens.

Installing Elasticsearch.

We can install elasticsearch two way

Install ES using repository

We sign all of our packages with the Elasticsearch Signing Key (PGP key D88E42B4, available from https://pgp.mit.edu) with fingerprint:

4609 5ACC 8548 582C 1A26 99A9 D27D 666C D88E 42B4

Download and install the public signing key:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

You need to install the apt-transport-http package on debian before proceeding.

sudo apt-get install apt-transport-http

Save the repository definition to /etc/apt/sources.list.d/elastic-8.x.list:

echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list


Note: These instructions do not use add-apt-repository for several reasons:
add-apt-repository adds entries to the system /etc/apt/sources.list file rather than a clean per-repository file in /etc/apt/sources.list.d
add-apt-repository is not part of the default install on many distributions and requires a number of non-default dependencies.
Older versions of add-apt-repository always add a deb-src entry which will cause errors because we do not provide a source package. If you have added the deb-src entry, you will see an error like the following until you delete the deb-src line:
Unable to find expected entry ‘main/source/Sources’ in Release file (Wrong sources.list entry or malformed file)

You can install the Elasticsearch Debian package with:

sudo apt-get update && sudo apt-get install elasticsearch

Download .deb files from official website.

The Debian package for Elasticsearch v8.10.4 can be downloaded from the website and installed as follows:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.10.4-amd64.deb
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.10.4-amd64.deb.sha512
shasum -a 512 -c elasticsearch-8.10.4-amd64.deb.sha512 
sudo dpkg -i elasticsearch-8.10.4-amd64.deb

Thant’s all for install elasticsearch in Ubuntu.

How to install & uninstall PHP 8.1 in Ubuntu

About PHP

PHP stands for Hypertext preprocessor, and it’s a script-based server side programing language. Using PHP we can show our data conditional based.

Installing PHP

The following command installs PHP using the apt package manager:

sudo apt install php8.1

Verify the installation

Using terminal we can verify PHP is installed or not. Just type this command on terminal

php -v

It’s showing the PHP version and PHP copyrights details. If that details not show it means PHP is not installed in your system.

Installing PHP modules

Mostly all the php module we can install using php<version>-<module_name>

Ex.

sudo apt install php8.1-gd

gd is one of the PHP module. We can install other modules with same syntax as per above.

Uninstall PHP module

Using this command you can uninstall any of the php module

sudo apt purge php8.1-gd
sudo apt autoremove
sudo apt autoclean
sudo apt update

This command remove PHP gd module. If you want to remove other module then just change the module name instead gd module.

Uninstall PHP

Using this command you can uninstall PHP.

sudo apt purge php8.1
sudo apt autoremove
sudo apt autoclean
sudo apt update

That’s all for install and remove PHP in ubuntu.

How to install composer in Ubuntu

About Composer

Composer is a popular dependency management tool for PHP, created mainly to facilitate installation and updates for project dependencies. It will check which other packages a specific project depends on and install them for you, using the appropriate versions according to the project requirements.

Requirments

Before install composer we need PHP. If you do not install PHP then first you need to install PHP in your ubuntu system.

If you need help to install PHP then click here for more info.

After installing PHP we can able to install composer in Ubuntu.

Installing Composer

Composer provides an installer script written in PHP. We’ll download it, verify that it’s not corrupted, and then use it to install Composer.

Make sure you’re in your home directory, then retrieve the installer using curl:

cd ~
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php

Next, we’ll verify that the downloaded installer matches the SHA-384 hash for the latest installer found on the Composer Public Keys / Signatures page. To facilitate the verification step, you can use the following command to programmatically obtain the latest hash from the Composer page and store it in a shell variable:

HASH=`curl -sS https://composer.github.io/installer.sig`

If you want to verify the obtained value, you can run:

echo $HASH
Output
e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a

Now execute the following PHP code, as provided in the Composer download page, to verify that the installation script is safe to run:

php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

You’ll see the following output:

Output
Installer verified

If the output says Installer corrupt, you’ll need to download the installation script again and double check that you’re using the correct hash. Then, repeat the verification process. When you have a verified installer, you can continue.

To install composer globally, use the following command which will download and install Composer as a system-wide command named composer, under /usr/local/bin:

sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

You’ll see output similar to this:

Output
All settings correct for using Composer
Downloading...

Composer (version 2.2.9) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

To test your installation, run this:

composer
Output
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version Composer version 2.2.9 2022-03-15 22:13:37
Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
...

That’s all for installing composer in Ubuntu.

How to install apache 2.x in ubuntu

About Apache

Apache is an open source web server that’s available for Linux servers free of charge.

In this tutorial we’ll be going through the steps of setting up an Apache server.

Install Apache 2 in Ubuntu

To install Apache, install the latest meta-package apache2 by running:

sudo apt update
sudo apt install apache2

Verify apache is installed or not

Open your borwser and just type http://localhost/ or http://<your_ip>/ and press enter.

it should show the apache default page.

That’s all, Apache 2 is installed in your ubuntu system.

How to install Magento 2.4.6 in Ubuntu

What is Magento ?

Magento is opensource e-commerce platform, which provides online merchants with a flexible shopping cart system. Magento offers powerful marketing, search engine optimization, and catalog-management tools.

Basic system requirment of Magento 2.4.6

Require these PHP extensions

  • ext-bcmath
  • ext-ctype
  • ext-curl
  • ext-dom
  • ext-gd
  • ext-hash
  • ext-iconv
  • ext-intl
  • ext-mbstring
  • ext-openssl
  • ext-pdo_mysql
  • ext-simplexml
  • ext-soap
  • ext-xsl
  • ext-zip
  • ext-sockets
  • ext-xml
  • ext-xmlreader
  • ext-xmlwriter
  • lib-libxml (DOMDocument)

Require Magento access keys

Magento access keys you can get from your account.

Magento Account > My profile > Marketplace >Access Keys

Download opensource Magento using composer

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .

Download Magento commerce using composer

composer create-project --repository-url=https://repo.magento.com/ magento/project-enterprise-edition .

Install any specific Magento version

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition:2.4.2

Give require permission to directory

We need to give correct permission to Magento directories by this below commands:

find . -type f -exec chmod 644 {} \;            
find . -type d -exec chmod 755 {} \;        
chmod -Rf 777 var
chmod -Rf 777 pub/static
chmod -Rf 777 pub/media
chmod 777 ./app/etc
chmod 644 ./app/etc/*.xml
chmod -Rf 775 bin

Create new database for Magento

We need to create empty database for new Magento setup.

mysql> CREATE DATABASE mag24;

Install Magento 2 using command

Before install we need to check elasticsearch is running and check MySQL server is running. Run the following lines in Command Line to install your Magento 2:

php bin/magento setup:install 
--base-url="http://local.magento24.com/" 
--db-host="localhost" 
--db-name="mag24" 
--db-user="root" 
--db-password="root" 
--admin-firstname="admin" 
--admin-lastname="admin" 
--admin-email="user@example.com" 
--admin-user="admin" 
--admin-password="admin123" 
--language="en_US" 
--currency="USD"
--timezone="America/Chicago" 
--use-rewrites="1" 
--backend-frontname="admin"

That’s all for installing Magento 2 in ubuntu.

How to create a custom module in Magento 2.x

Today we are going to create a simple module in Magento 2. We display a simple message using the controller and using a custom layout.

So, In this module we need to create one controller and a custom route. Using this route we are able to see our simple message in magento.

Also, We need to create the layout file for displaying simple messages using layout.

Our module vendor is RS and the module name is FirstModule.

Let’s start creating a simple module in Magento 2.x.

1. Create module directory

First we need to create a vendor directory, Our vendor name is RS. So, We need to create an RS directory under your app/code directory.

After creating the vendor directory we need to create a module directory. Our module name is FirstModule. So, we need to create FirstModule under the app/code/RS/ directory.

In my case the module directory looks like MAGENTO_ROOT/app/code/RS/FirstModule/. But in your case you need to create a directory as per your vendor name and module name.

Ex. your vendor name is  Abc and module name is Hello the your module file looks like MAGENTO_ROOT/app/code/Abc/Hello/.

2. Create module registration.php file & What is the use of this file?

This file helps to register our component to our Magento 2 application. Magento 2 have different type of component like Module, Theme, Langauge and Library. We have register our module component to Magento 2.

<?php
//file path: MAGENTO_ROOT/app/code/RS/FirstModule/registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'RS_FirstModule',
    __DIR__
);


3. Create module config file

This file helps to config the module version and module dependencies.

<?xml version="1.0"?>
<!-- file path: MAGENTO_ROOT/app/code/RS/FirstModule/etc/module.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="RS_FirstModule" setup_version="1.0.0" />
</config>

4. Create custom frontend route config file

This file registered our custom route to Magento application. We have define two attributes in this file. id and frontName, using id we can create our custom layout. frontName is represent the first part of url.

<?xml version="1.0"?>
<!-- file path: MAGENTO_ROOT/app/code/RS/FirstModule/etc/frontend/routes.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="first_module" frontName="first_module">
            <module name="RS_FirstModule" />
        </route>
    </router>
</config>

5. Create controller for display custom message without layout

Created the controller for display the simpple message in Magento 2.x. I have just print simple message using echo function.

<?php
//file path: MAGENTO_ROOT/app/code/RS/FirstModule/Controller/First/Index.php
namespace RS\FirstModule\Controller\First;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

class Index extends Action
{
    public function __construct(Context $context)
    {
        return parent::__construct($context);
    }
    public function execute()
    {
        echo __("This is my first Magento 2 module");
    }
}

6. Install your module to magento

We have create custom module for print simple message without using XML layout. Above all the command is responsible to enable and install our module to Magento 2.

php bin/magento module:enable RS_FirstModule
php bin/magento setup:di:compile
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f

After that run the setup:upgrade and setup:static-content:deploy -f command to install module.

7. Check your first module message

Let’s verify our module is working fine or not.

How we can check our module in browser?

Our custom route is first_module.

Our controller name is First.

Our action name is Index.

Our url looks like first_module/first/index. Using this url we can check our module result.

Open this URL(http://your_magento_url/first_module/first/index) on your browser

Output:

8. Now we will display this message using Magento layout. So we need to some changes to our existing controller

Modified Index.php file for load simple message using custom layout. I have added new dependency in constructor, this new argument is responsible to load custom layout.

<?php
//file path: MAGENTO_ROOT/app/code/RS/FirstModule/Controller/First/Index.php
namespace RS\FirstModule\Controller\First;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action
{
    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * Controller constructor
     *
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        return parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

9. Create layout file

Create custom layout file. Using id we can create the custom layout file name. Our route id is first_module and our controller name is First and action name is Index. Our layout file look like first_module_first_index.xml.

In the simple module we not need any dynamic value in template file. So, I just use default magento template block Magento\Framework\View\Element\Template.

<?xml version="1.0"?>
<!-- file path: MAGENTO_ROOT/app/code/RS/FirstModule/view/frontend/layout/first_module_first_index.xml -->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="page.main.title">
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">First Module</argument>
                </action>
        </referenceBlock>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="message" template="RS_FirstModule::message.phtml" />
        </referenceContainer>
    </body>
</page>

10. Create template file for display message

<?php 
// file path: MAGENTO_ROOT/app/code/RS/FirstModule/view/frontend/templates/message.phtml 

/**
 * Product list template
 *
 * @var $block \Magento\Framework\View\Element\Template
 * @var \Magento\Framework\Escaper $escaper
 */
?>
<?= $escaper->escapeHtml(__("This is my first Magento 2.x Module")); ?>

Clear Magento cache and check message with the same URL(http://your_magento_url/first_module/first/index)

Output:

That’s all for the simple module with and without layout in Magento 2.