Before we start the installation process of Magento 2, first we need to understand what are the prerequisites that we need to have in our system. The following URL will show you system requirements table along with the combination versions of softwares you need to install the Magento 2
https://experienceleague.adobe.com/docs/commerce-operations/installation-guide/system-requirements.html
After thorough research, one of the combinations of versions that I am going to use in this article. To install and run Magento application we need following applications in our system (in this scenario it is windows operating system)
- Server – (here I am going to choose Apache),
- PHP
- Database – (here I am going to choose MySQL, you can choose any database but you need to configure your Magento accordingly).
- Search Engine – (here I choose elastic search, you can choose any search engine, again you need to configure your Magento accordingly)
- Composer – Package management
Here I can choose to either install Server, PHP and Mysql Separately or I can install XAMP which actually provides all these three as a package.
You can download the XAMPP from the following link
- Open a link www.apachefriends.org
- Click on the Downloads button from the top menu
- https://www.apachefriends.org/download.html
- You will see three sections one for each operating system, under windows operating system by default you should be seeing the latest version of XAMPP, as of today it is (8.0.25, 8.1.12 & 8.2.0) Click on the “More Downloads” link under windows OS section to look out for different version.
- https://sourceforge.net/projects/xampp/files/
- Now you should see a folder called “XAMPP Windows” folder, click on that and then click on 7.4.30.
- Install the XAMPP just like any other windows software, just remember where you are storing your XAMPP folder for future reference.
- The reason that I have taken the 7.4.30 file is for compatibility issues, you can try and explore different versions combinations, here in this XAMP version you will get PHP 7.4.30
2.1 Our next step is making PHP globally available, which means if you run php -v command in the PowerShell/command prompt you should see php version.
To achieve this we need to add a PHP path to environmental variable windows. First, you need to identify the PHP path,
If you have installed your XAMPP in C\XAMPP drive the PHP path would be C:\XAMPP\php, in some cases, you should see the bin folder PHP. End of the day whether you installed your PHP using XMAPP or separately you should always point your path to PHP executable file.
- Now you add that PHP path to the Windows Environment Variable Path
- On the Windows taskbar, right-click the Windows icon and select System.
- In the Settings window, under Related Settings, click Advanced system settings.
- On the Advanced tab, click Environment Variables.
- Click New to create a new environment variable. Click Edit to modify an existing environment variable.
- After creating or modifying the environment variable, click Apply and then OK to have the change take effect.
After you successfully add the PHP path, close and open the PowerShell if it is opened and then try the command php -v, in any path of the windows you should be able to see the PHP version.
2.2 Our next step is to install and enable all the necessary extensions of php to support the Magento2 installation and running.
Following are the extensions you need to enable
gd2, intl, soap, xsl, sockets, sodium
- These extensions can be enabled under php.ini file, you can get to this file in two ways
- Under XAMPP control panel next to Apache, you will find the config button, click on that you see php.ini click on that it will open php.ini file
- You can open the file from C:\XAMPP\php\php.ini
- Once you open the file, and search for gd or gd2 you will see the extension is commented using a semicolon (;extension=gd2), remove that semicolon (extension=gd2) which will enable the extension, and you need to do the same process for all extensions mentioned above.
- Copy php/libsodium.dll to apache/bin/
- Restart the apache server by clicking stop and start on XAMP control panel.
You also need to increase the memory limit under the same php.ini file
- max_execution_time=18000
- max_input_time=1800
- memory_limit=4204M
Since we made PHP global, now we can install composer in our system using php commands, you can install composer using an exe file. I am going to install composer using the command line. First I will create a folder under C:\ComposerFile, get into this folder in your PowerShell or command prompt and then run the below commands
Step 3.1 Installing Composer using command line with php prerequisites
You need to run the following three commands, I will also explain what each command means. The source of these commands is from https://getcomposer.org/download/
First Command
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
###############
##To download composer-setup.php.
###############
Second Command
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
###############
##To simply verify the downloaded composer-##setup.php wether the file is really downloaded from the ##getcomposer.org or not
###############
Third Command
php composer-setup.php
###############
## the above command will create composer.phar when we compile it using php command. You can keep this file some where safe for future reference
###############
####Now you can run command
composer.phar -V
##this command will show you the version of the composer.
Now your composer is successfully installed in your system but it is just that every time you need to use composer.phar if you want to run any composer command, sometimes if you are in a different folder from where composer.phar file exists you need to give the full path of the composer.phar file to run the composer command which will be annoying so we need to make composer global just like we did with php
Step 3.2 - Making sure our composer.phar is globally available and instead of calling composer.phar every time we can we can call only composer to run any composer commands.
To achieve this first we need to convert our composer.phar to composer.bat, you need to make sure where ever you are running any one of the following command your composer.phar file should exist we are creating composer.bat file from composer.phar. In our case we are running underC:\ComposerFile. The source of following commands are https://getcomposer.org/doc/00-intro.md#installation-windows
###Using cmd.exe: If you are using command prompt
C:\ComposerFile> echo @php "%~dp0composer.phar" %*>composer.bat
###Using PowerShell: If you are using PowerShell
C:\ComposerFile> Set-Content composer.bat '@php "%~dp0composer.phar" %*'
After running the command you should be able to see composer.bat file under C:\ComposerFile folder, now you need to add this path(C:\ComposerFile) to Environment Variables. Please follow same process that is mentioned under PHP above environment variables.
Now you should be able to run the composer command from any path in your PowerShell or Command prompt.
And one final command we need to run, this is to downgrade our composer to 1, the reason we are downgrading is there is an issue with PHP8 and libsodium library, and composer2 runs on PHP8 but our PHP version is PHP7, composer 1 runs in PHP 7, so I am using composer 1 for my Magento installation process. You can use different software combination versions.
Following is the command you can use, this command will install last latest version of composer1
composer self-update --1
Step 4.1 - Installing elastic search engine
I choose the elastic search 7 version, you can download it from the following link
https://www.elastic.co/downloads/past-releases/elasticsearch-7-17-9
Since we are working on the Windows operating system, please download the Windows file, after you download it, you can extract the zip file, rename the folder to elasticsearch7 and keep this folder under C:\elasticsearch7.
- Now you can do either 4.2 or 4.3, you can get information about both ways in the following links
- 4.2 – https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-windows.html#windows-running
- 4.3 – https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-windows.html#windows-service
Step 4.2 - Running elastic search directly
In this way, you can simply run the elasticsearch.bat file from the bin folder from either PowerShell or Command prompt. Get into folder C:\elasticsearch7 and run the following command
.\bin\elasticsearch.bat
### here you need to keep open the Powershell, you can minimize it but not close it since elastic search is running, but if you use step 4.3 this is not required
Step 4.3 - Running elastic search as a service
- Install Elasticsearch as a service. The name of the service and the value of
ES_JAVA_HOME
will be made available during install:
- C:\elasticsearch7\bin>elasticsearch-service.bat install
- Start Elasticsearch as a service. When Elasticsearch starts, authentication is enabled by default:
- C:\elasticsearch7>.\bin\elasticsearch-service.bat start
After successful running of elastic search using either 4.2 or 4.3 you should be able to see a JSON object with a elastic search engine information when you run localhost:9200(Please make sure that the 9200 port is available before you start the elastic search, usually it will be available)
You need to start elastic search whenever it stopped using the very same command. You have stop, remove, and manager commands available in the very same link provided above for both 4.2 and 4.3 in bullet points, feel free to explore.
Step 5.1 - Please create adobe id and login into marketplace.magento.com with that adobe id.
This is where you need to create adobe id and login into marketplace.magento.com using adobe id. Once you login you need to top right side where you see your name click on that, dropdown with two menu options will appear (signout, myprofile), click on “My Profile”.
Now you see under “My Products-> Access Keys”. Create a New access key with any name by clicking on the button “Create New Access Key”.
Once after giving a name to access key and click on save you should see public key and private key these are your username and password which will be asked when you install magento2 using composer command in Step 5.2
Step 5.2 - Downloading Magento 2.4.5 using composer command
You can get information about composer create command for Magento 2 to download Magento community or commerce version in the following link
https://experienceleague.adobe.com/docs/commerce-operations/installation-guide/composer.html?lang=en
Following is the command that will be used to download the Magento 2 community edition. I will create a folder called “MagentoProjects” in the desktop of the windows and I will enter into it using either PowerShell or Command Prompt and run the following command
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition:2.4.5 magentoone
In the above command “Magentoone” is the folder name which I have given as a project name and this is where all the Magento files will be downloaded. When the download starts it will ask for the user and password. Please enter the username(public Key) and password(Private Key) which you created in Step 5.1
Step 6.1 Enabling the following modules if they are not in C:\XAMPP\apache\conf\httpd.conf
Once you open this file you should be seeing following modules in httpd.conf
Find below lines and change them from
#LoadModule version_module modules/mod_version.so
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
#LoadModule rewrite_module modules/mod_rewrite.so
#Include conf/extra/httpd-vhosts.conf
To (Removing # means you are uncommenting them so they will be enable)
LoadModule version_module modules/mod_version.so
LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
Include conf/extra/httpd-vhosts.conf
Step 6.2 - Configuring your virtual host in your apache vhosts file
Once you done with these changes you need to open another file from C:\XAMPP\apache\conf\extra\httpd-vhosts.conf and add following code to it. This is a virtual host setup where you can create a host for your Magento application, it may change depending on your server. We are actually creating localhost domain names with localhost and port. For our example we have taken 8081, creating virtual host with 8081 port is not enough you need to make sure you tell apache to expose that port number using. Listen 8081
You also see here that Directory tag added inside virtual host, this is very crucial for your website to run, you are allowing apache server to access the root folder using this tag
Listen 8081
<VirtualHost *:8081>
ServerAdmin engage@vlpreddy.com
DocumentRoot "C:\Users\user\MagentoProjects\Magento\magentoone\pub"
##ServerName dummy-host2.example.com
ErrorLog "C:\Users\user\Desktop\MagentoProjects\magentoone\error.log"
CustomLog "C:\Users\user\Desktop\MagentoProjects\magentoone\access.log" common
<Directory C:\Users\user\Desktop\MagentoProjects\magentoone>
AllowOverride All
Options Indexes FollowSymLinks
Require all granted
</Directory>
</VirtualHost>
######
C:\Users\user\Desktop\Magento\magentoone\pub. -> this is the root folder of the magento application, it's been like this since 2.4, before 2.4 the root folder was directly "magentoone" folder.
Note : Make sure what ever the path your are using under virtual host is same as the path where you have downloaded the magento using composer command in the above Step 5
######
and stop and start the apache using XAMPP control panel
Step 7.1 - Creating database, user and assign user to database
Goto localhost/phpmyadmin ,
- Create database by clicking on Database menu
- Create a user –
- First click on a created database which listed in the left side
- check for the “privileges” link on the top menu next to “operations” link, if it is not visible click on the “more” link you should see the “privileges” link under the menu
- At the bottom you see the “Add user account” link, with this you can create new user, make sure you select a check box saying “grant all privileges on database <what ever you have selected at the first bullet point in the list>”. This check box will only appear when you click on “privileges” after you click on the selected database in this case its “magentoone”
Step 7.2(optional) - Configuring mysql if re-indexing runs slow while running Magento commands
You need to update following options under C:\XAMPP\mysql\bin\my.ini
long_query_time = 1
innodb_buffer_pool_size = 5024
innodb_thread_concurrency = 4
innodb_flush_log_at_trx_commit = 2
thread_cache_size = 32
query_cache_size = 64M
query_cache_limit = 2M
join_buffer_size = 8M
tmp_table_size = 256M
key_buffer = 32M
innodb_autoextend_increment = 512
To avoid slow reindex(my.ini)
optimizer_switch='rowid_filter=off'
optimizer_use_condition_selectivity = 1
Step 8.1 - Before you run php Magento install command you need to fix few things
We need to run php install command but we need update the code in the following files, since we already know these issues will come while installing Magento so we fix these files first and then run php command. Please follow instructions for the following three. I am also defining what errors we might get if we don’t fix these changes. All the paths you see below are from Magento root folder “Magentoone”
Error 1 - Gd2.php file error
Solution:
Find "validateURLScheme" function in vendor\magento\framework\Image\Adapter\Gd2.php file. at line 92. Replace function with this:
private function validateURLScheme(string $filename) : bool
{
$allowed_schemes = ['ftp', 'ftps', 'http', 'https'];
$url = parse_url($filename);
if ($url && isset($url['scheme']) && !in_array($url['scheme'], $allowed_schemes) && !file_exists($filename)) {
return false;
}
return true;
}
Error 2 - You website css and javascript won't work properly
Solution :
Go to: vendor\magento\framework\View\Element\Template\File
Edit Validator.php using a text editor and find this line:
instead of the line
strpos($realPath, $directory)
use
strpos($path, $directory)
Error 3 - You website css and javascript won't work properly
Solution:
Open up app/etc/di.xml in the editor,
– Find the path “Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink” and replace to “Magento\Framework\App\View\Asset\MaterializationStrategy\Copy”
Once you done with all changes you can open the PowerShell or Command Prompt go to the Magento root folder which is C:\Users\users\Desktop\MagentoProjects\Magentoone and run the following command
Step 8.2 - Installing Magento using PHP Magento Install command
php bin/magento setup:install --base-url=http://localhost:8081 --db-host=localhost --db-name=magentoone --db-user=leela1 --db-password=Red123# --admin-firstname=admin --admin-lastname=admin --admin-email=admin@admin.com --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1 --search-engine=elasticsearch7 --elasticsearch-host=localhost --elasticsearch-port=9200 --elasticsearch-timeout=15
Once after successfully completion of the above command you need to run the following commands one by one, some commands may take a while, so not to worry wait until all these commands complete.
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento indexer:reindex
php bin/magento cache:flush
Error 4 : When you run php bin/magento setup:di:compile
command if you get this error (Plugin Generator error)
Solution:
Open PluginListGenerator.php file located at vendor/magento/framework/Interception.
Go to line no 158 or find the word “scopePriorityScheme” in PluginListGenerator.php file.
Replace the line
"$cacheId = implode('|', $this->scopePriorityScheme) . "|" . $this->cacheId;" with"$cacheId = implode('-', $this->scopePriorityScheme) . "-" . $this->cacheId;"
and run the compile command again, after that complete remaining commands static-content, reindex, flush
That’s it that’s the end of the Magento Installation process in windows.
- Now you can access
- your store website under localhost:8081
- You admin under localhost:8081/admin (after 2.4.3 i guess it will show admin url after success install).
- You can also get admin url by running following command
- php bin/magento info:adminuri
When you try to access your admin using the credentials your enter while running install command your admin fails to login, to resolve this issue you need to disable the twofactor authentication by running following command
- php bin/magento module:disable Magento_TwoFactorAuth
- php bin/magento cache:flush