Tag Archives: Setup

Magento 2: setup upgrade error “We can’t find the role for the user you wanted”

open the file
vendor/magento/module-authorization/Model/Acl/AclRetriever.php and replaced the below code

if (!$role) {
throw new AuthorizationException(
__('We can\'t find the role for the user you wanted.')
);
}
$allowedResources = $this->getAllowedResourcesByRole($role->getId());

with

if (!$role) {
$allowedResources = array();
}

and revert it on sucess.

magento setup upgrade Unexpected error. Unable to create oAuth consumer account.

I have encountered this issue while community to enterprise migration and it was happening since the base table ‘oauth_consumer‘ was not available.

CREATE TABLE oauth_consumer (   entity_id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity Id',
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Created At',
updated_at timestamp NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT 'Updated At',
name varchar(255) NOT NULL COMMENT 'Name of consumer',
key varchar(32) NOT NULL COMMENT 'Key code',
secret varchar(32) NOT NULL COMMENT 'Secret code',
callback_url text COMMENT 'Callback URL',
rejected_callback_url text NOT NULL COMMENT 'Rejected callback URL',
PRIMARY KEY (entity_id),
UNIQUE KEY OAUTH_CONSUMER_KEY (key),
UNIQUE KEY OAUTH_CONSUMER_SECRET (secret),
KEY OAUTH_CONSUMER_CREATED_AT (created_at),
KEY OAUTH_CONSUMER_UPDATED_AT (updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='OAuth Consumers';

Setup GZIP compression CentOS Linux

GZIP compression has serious impact on performance and your website loading time. In order to use it make sure that mod_deflate and mod_filter are enabled.

First create new file that will contain GZIP settings for Apache:

sudo vi /usr/local/apache2/conf/extra/httpd-deflate.conf

Paste there following content and save the file:

<IfModule mod_deflate.c>
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/ecmascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
</IfModule>
</IfModule>

It will add GZIP compression to most popular file types such as HTML, CSS, JS etc. If you need anything else that requires compressing, simply add more MIME types.

Now you need to include it to main Apache configuration. so open httpd.conf and Include httpd-deflate.conf.

vi /usr/local/apache2/conf/httpd.conf and make sure the following line is added.

Include conf/extra/httpd-deflate.conf

Magento 2 Setup Multiple Websites

Follow the below steps to configure multiple magento websites

1) Create websites, stores, and store views in the Magento Admin

2) Create Apache virtual hosts.

Edit the file E:\xampp\apache\conf\extra\httpd-vhosts.conf

<VirtualHost *:80>
 ServerName phpcode.com
 DocumentRoot E:\xampp\htdocs\phpcode\pub
 </VirtualHost>

<VirtualHost *:80>
 ServerName in.phpcode.com
 DocumentRoot E:\xampp\htdocs\phpcode\pub
 SetEnv MAGE_RUN_CODE "india"
 SetEnv MAGE_RUN_TYPE "website"
 </VirtualHost>

3) Restart Apache.

4) Edit your vhost file(In my system its C:\Windows\System32\drivers\etc\hosts)

127.0.0.1 phpcode.com
127.0.0.1 in.phpcode.com

5) Verify the sites accessing the following URLs

phpcode.com
in.phpcode.com

Recurring Magento 2

Recurring script can be used to run some script that need to executed whenever we issue php bin/magento setup:upgrade irrespective of version change.

The recurring script will be run after the module setup script every time the command line

php bin/magento setup:upgrade

This script will be defined same as InstallSchema class but difference in name of the class. The example for this class you can see in vendor/magento/module-indexer/Setup/Recurring.php

InstallData Magento 2

The InstallData will be run during the module install and its used to load initial data to DB table.

The module install script will run when you run the following command line

php bin/magento setup:upgrade

File : app/code/PHPCodez/First/Setup/InstallData.php

<?php
 namespace PHPCodez\First\Setup;
 use Magento\Framework\Setup\InstallDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;
 class InstallData implements InstallDataInterface {
   public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
   $setup->startSetup();
   $setup->getConnection()->query("INSERT INTO phpcodez_post SET name = 'Post 1'");
   $setup->endSetup();
 }
 }

InstallSchema Magento 2

The InstallSchema will be run during the module install and its used to cretae DB table structure.

The module install script will run when you run the following command line

php bin/magento setup:upgrade

File : app/code/PHPCodez/First/Setup/InstallSchema .php

<?php
namespace PHPCodez\First\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallSchema implements InstallSchemaInterface
{
 public function upgrade( SchemaSetupInterface $setup, ModuleContextInterface $context ) {
 $installer = $setup;

$installer->startSetup();

if(version_compare($context->getVersion(), '1.1.0', '<')) {
 if (!$installer->tableExists('phpcodez_post')) {
 $table = $installer->getConnection()->newTable(
 $installer->getTable('phpcodez_post')
 )
 ->addColumn(
 'post_id',
 \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
 null,
 [
 'identity' => true,
 'nullable' => false,
 'primary' => true,
 'unsigned' => true,
 ],
 'Post ID'
 )
 ->addColumn(
 'name',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 255,
 ['nullable => false'],
 'Post Name'
 )
 ->addColumn(
 'created_at',
 \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
 null,
 ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
 'Created At'
 )->addColumn(
 'updated_at',
 \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
 null,
 ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
 'Updated At')
 ->setComment('Post Table');
 $installer->getConnection()->createTable($table);

$installer->getConnection()->addIndex(
 $installer->getTable('phpcodez_post'),
 $setup->getIdxName(
 $installer->getTable('phpcodez_post'),
 ['name'],
 \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
 ),
 ['name'],
 \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
 );
 }
 }

$installer->endSetup();
 }
}