Blog

How to create new module in Magento 2

Create new module Magento 2, you need to follow step by step in this guide:

  • Step 1: Create folder of new Module
  • Step 2: Create file etc/module.xml
  • Step 3: Create file registration.php
  • Step 4: Run upgrade and check again 

Step 1: Create folder of new Module
The name of a module in magento is defined by VendorName_ModuleName. Go to path :
{path_root_folder_magento}/app/code. This is a area which storage the modules of site.
Create folder VendorName/ModuleName
Example: Atsharing/DemoModule
Step 2: Create file etc/module.xml
After step 1, go to the VendorName/ModuleName and create file etc/module.xml with demo content:

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Atsharing_DemoModule" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>


Note:

 - name: name of module. Follow this structure VendorName_ModuleName.
 - setup_version: version module.
 - sequence: in sequence tag list modules. This param, i will discuss it in the later article.
Step 3: Create file registration.php
In this step, create file registration.php with demo content:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Atsharing_DemoModule',
__DIR__
);

Step 4: Run upgrade and check again 

Run setup upgrade cmd:
Go to the root magento path, open terminal and run commandline:

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy -f

How to check in the module is created ?

 - Check file {path_root_folder_magento}/app/etc/config.php and find your module
 - Connect database and view table module_setup. Find name of module.

Download Module Demo here!

Adminer not login by root

If you cann't connect mysql by account root on adminer, i hope the article to help you.

Step 1: Connect in sudo mysql via command line. 

sudo mysql -u root

Step 2: After that, check your accounts present in your database.

SELECT User,Host FROM mysql.user;

Step 3: Delete current root@localhost account.

DROP USER 'root'@'localhost';

Step 4: Recreate your user.

CREATE USER 'root'@'%' IDENTIFIED BY 'your_pass';

Step 5: Give permissions to your user (don't forget to flush privileges).

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

FLUSH PRIVILEGES;

Step 6: Exit MySQL and try to reconnect on adminer.

Install Linux, Nginx , MariaDB, PHP.

This article will guide to setup and configure basic Web development environment.

Step 1: Nginx
sudo apt install nginx

Then run the following commands to nginx always start when the computer is turned on. sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

See all logs of nginx via commands: sudo gedit /var/log/nginx/error.log

Step 2: MariaDB
MariaDB is more advanced, more widely supported and more popular on the server than Mysql.

sudo apt-get install mariadb-server mariadb-client

Similar to MariaDB start when starting Ubuntu

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service
Run the following commands in turn to install and configure MariaDB at the machine
sudo mysql_secure_installation
Note: The root password is simply set, so it matches the password ubuntu (123456) to avoid forgetting to re-install it when reinstalling.

Enter current password for root (enter for none): press enter.
Set root password? [Y/n]: Y
New password: 123456
Re-enter new password: 123456
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]:  Y
Reload privilege tables now? [Y/n]:  Y

Restart MariaDB
sudo systemctl restart mysql.service

To check if MariaDB is working, open the Terminal and login MariaDB Console

sudo mysql -u root -p


Step 3: PHP 7.1 or 7.2
Recommend using php7.1 because it will be compatible with more 2 magento versions.
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Install php7.1 and the necessary libraries for magento 2
sudo apt install php7.1 php7.1-common php7.1-mbstring php7.1-xmlrpc php7.1-soap php7.1-gd php7.1-xml php7.1-intl php7.1-mysql php7.1-cli php7.1-mcrypt php7.1-zip php7.1-curl php7.1-bcmath

Or install php 7.2

sudo apt install php7.2 php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-soap php7.2-gd php7.2-xml php7.2-intl php7.2-mysql php7.2-cli php7.2-zip php7.2-curl php7.2-bcmath

Configure, Linux, Apache 2, MariaDB, PHP.

This article will guide to setup and configure basic Web development environment.

Step 1: Apache 2
sudo apt install apache2

Then run the following commands to apache2 always start when the computer is turned on.
sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service
After running, go to http: // localhost or 127.0.0.1 to check, if there is an apache screen, the installation is successful.

 

Step 2: MariaDB
MariaDB is more advanced, more widely supported and more popular on the server than Mysql.

sudo apt-get install mariadb-server mariadb-client

Similar to MariaDB start when starting Ubuntu

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service
Run the following commands in turn to install and configure MariaDB at the machine
sudo mysql_secure_installation
Note: The root password is simply set, so it matches the password ubuntu (123456) to avoid forgetting to re-install it when reinstalling.

Enter current password for root (enter for none): press enter.
Set root password? [Y/n]: Y
New password: 123456
Re-enter new password: 123456
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]:  Y
Reload privilege tables now? [Y/n]:  Y

Restart MariaDB
sudo systemctl restart mysql.service

To check if MariaDB is working, open the Terminal and login MariaDB Console

sudo mysql -u root -p
albert


Step 3: PHP 7.1 or 7.2
Recommend using php7.1 because it will be compatible with more 2 magento versions.
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Install php7.1 and the necessary libraries for magento 2
sudo apt install php7.1 libapache2-mod-php7.1 php7.1-common php7.1-mbstring php7.1-xmlrpc php7.1-soap php7.1-gd php7.1-xml php7.1-intl php7.1-mysql php7.1-cli php7.1-mcrypt php7.1-zip php7.1-curl php7.1-bcmath

Or install php 7.2

sudo apt install php7.2 php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-soap php7.2-gd php7.2-xml php7.2-intl php7.2-mysql php7.2-cli php7.2-zip php7.2-curl php7.2-bcmath



After installing the above steps, proceed to configure php to optimize for Magento 2

Run command: sudo gedit /etc/php/7.1/apache2/php.ini
Locate and modify

file_uploads = On
allow_url_fopen = On
memory_limit = 1024M
upload_max_filesize = 100M
post_max_size = 100M
max_input_time = 360
max_execution_time = 360

Save file end restart apache 2: sudo service apache2 restart

Create one file phpinfo.php in folder /var/www/html/phpinfo.php

code
echo phpinfo();
If you see the PHP parameter screen displayed, it means that the PHP code is already running

Copyright © 2020-present atsharing.com, Inc. All rights reserved.