Magento 2 Installation Guide: Setup, Common Issue Resolution, and Optimization

Table of Content

Magento 2 installation guide

Note: We've updated this article in 2024 to reflect the latest approaches and possible issues.

Amasty's team first decided to install Magento 2 as soon as its 0.42.0-beta1 version emerged in 2014. Since then, we’ve performed hundreds of thousands of Magento 2 installations for our clients. 

That’s why we decided to provide an in-depth Magento 2 installation guide, addressing common best practices (including the Composer approach), and possible errors, and offering solutions. This comprehensive guide covers key steps for a successful Magento 2 installation, from database creation to post-installation tasks.

Magento: How to Install

There are several distinct methods to install Magento, each offering a unique approach. The first involves the hands-on process of cloning the Magento 2 codebase from GitHub. This method, providing direct access to the latest GitHub code, caters to those who have a decent understanding of the technical side of Magento development.

On the other hand, the Composer-based installation method is more user-friendly, allowing the setup with a single command. This method relies on Composer's dependency management, ensuring a more streamlined, modular, and effortlessly maintainable system. Users simply initiate the process, specifying parameters like the Magento repository and installation directory.

While both methods share the common goal of installing Magento 2, the Composer approach offers more efficiency and an overall simpler configuration. In this Magento 2 installation guide, we’ll use the Composer approach for your convenience.

1. Creating a Database

Creating a database for your Magento 2 setup ensures a smooth configuration for the future Magento instance. Follow these steps:

1. Log in to your MySQL server using a tool like phpMyAdmin or the command line.

2. Execute the following SQL command to create a new database. Replace <database_name> with your preferred database name.

CREATE DATABASE `<database_name>` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

3. Create a user and set a secure password. Replace <username> with your desired username and <password> with a strong, unique password.

CREATE USER '<username>'@'localhost' IDENTIFIED BY '<password>';

4. Grant all privileges to the user on the newly created database.

GRANT ALL PRIVILEGES ON `<database_name>`.* TO '<username>'@'localhost';

5. Flush the privileges to apply the changes.FLUSH PRIVILEGES;

6. Exit MySQL: If you used the command line, exit the MySQL prompt.

EXIT;

Make sure you replace <database_name>, <username>, and <password> with your chosen values. Keep these credentials secure, as they will be used during the Magento 2 installation process. With the database configured, you are now ready to proceed further with the Magento 2 installation.

2. Composer Magento Installation

Before installing Magento, make sure that Composer is set up on your server. Use it to download the Magento 2 codebase. Execute the following commands in your terminal or command prompt:

composer create-project --repository-url=https://repo.magento.com/
magento/project-community-edition <install-directory-name>

Replace <install-directory-name> with the preferred directory name for your Magento installation. This step initiates the Composer-based download of Magento 2, paving the way for a seamless installation process.

3. Web Server Configuration: Apache or Nginx

Configure your chosen web server, be it Apache or Nginx, to direct to the Magento 2 installation directory.

Follow these instructions for a seamless setup:

For Apache:

Locate your Apache configuration files, often found in the /etc/apache2/sites-available/ directory. Create a new virtual host configuration file for your Magento 2 installation using the <VirtualHost> directive. Then, specify the necessary configurations such as ServerName, DocumentRoot, and other relevant settings.

Example:

ServerAdmin [email protected]
ServerName yourdomain.com
DocumentRoot /var/www/html/magento2
<Directory /var/www/html/magento2>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

Use the a2ensite command to enable the virtual host configuration:

sudo a2ensite your_virtual_host_config_file.conf

Restart the Apache web server to apply the changes:

sudo systemctl restart apache2

For Nginx:

Locate your Nginx configuration files, often found in the /etc/nginx/sites-available/ directory.

Create a new server block configuration file for your Magento 2 install location using the server block. Specify configurations such as server_name, root, and other relevant settings.

Example:

server {
listen 80;
server_name yourdomain.com;
root /var/www/html/magento2;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Create a symbolic link to enable the server block configuration:

.sudo ln -s /etc/nginx/sites-available/your_server_block_config_file /etc/nginx/sites-enabled/

Restart the Nginx web server to apply the changes:

sudo systemctl restart nginx

Need Help with Installing Magento?

Reach out to Amasty's Magento team for a consultation or hands-on assistance.

4. How to Install Magento: Executing Setup Commands

Once you've configured your server and completed the necessary pre-installation steps, it's time to execute the setup commands to finalize the installation of Magento 2 using Composer. Follow these steps:

Open your command-line interface (CLI) and navigate to the root directory of your Magento installation using the cd command:

cd /path/to/your/magento/installation

Replace "/path/to/your/magento/installation" with the actual path where Magento 2 is installed.

Run the setup commands to initiate the installation process. The primary command is:

bin/magento setup:install

This command triggers the automatic Magento guide and prompts you to provide essential information such as database connection details, base URL, administrative credentials, and more.

During the Magento 2 installation process, you'll be prompted for authentication credentials. You need a set of Magento Marketplace keys for this step. If you don't have these keys, create a Magento Marketplace account, navigate to the "My Profile" section, and generate the keys in the "Access Keys" tab.

Composer allows you to customize your Magento 2 setup. Explore options like choosing sample data inclusion, specifying your desired PHP version, and more. Refer to the Composer documentation for additional customization possibilities.

Pay careful attention to any errors or warnings that may appear during the installation process. Addressing these issues promptly ensures a smooth and successful Magento setup.

5. How to Install Magento 2: Overcoming Stalling Error 

During our initial attempt to install Magento using the web interface, we encountered a well-documented problem associated with the xdebug PHP extension. This issue manifested in the form of a PHP Fatal error due to the 'xdebug.max_nesting_level' reaching its limit. To resolve this roadblock, follow these steps:

1. Open the configuration file for your PHP installation. This can typically be found at: '/etc/php/7.x/cli/conf.d/20-xdebug.ini', where '7.x' represents your PHP version.

2. Add or modify the following line to adjust the 'xdebug.max_nesting_level' value:

xdebug.max_nesting_level = 500

Setting this value to 500 allows for a deeper nesting level, addressing the error during installation. Save the changes and close the file. With this adjustment, proceed with the installation using the following command in your Magento 2 root directory:

cd /var/www/magento2 php -d xdebug.max_nesting_level=500 -f bin/magento setup:install
--base_url=http://magento2.local/ --backend_frontname=admin
--db_host=mysql56.local --db_name=magento2 --db_user=magento2
--db_pass=magento2 --admin_firstname=Local --admin_lastname=Admin
[email protected] --admin_username=admin
--admin_password=adminpswd --language=en_US
--currency=USD --timezone=America/Chicago

By adjusting the 'xdebug.max_nesting_level' and executing the installation via CLI, you can successfully navigate past common errors and ensure a smooth Magento 2 installation process.

Additional Configuration: .htaccess Modification

If you have xdebug installed, make specific adjustments to the .htaccess file to guarantee a smooth installation process. Follow these steps to ensure proper functionality and prevent other potential errors:

Open the .htaccess file located in your Magento 2 root directory using a text editor.

Add the following lines at the end of the file:

<IfModule mod_php7.c>
php_value xdebug.max_nesting_level 500
</ifmodule>

Save the changes to the .htaccess file. These lines set the 'xdebug.max_nesting_level' value to 500, addressing a known issue related to the xdebug PHP extension. This modification ensures that the Magento 2 installation process proceeds smoothly without encountering nesting level errors.

6. Navigating and Troubleshooting Other Installation Challenges  

During the Magento 2 installation process, users may encounter common challenges. This section provides insights and solutions to help you navigate through potential issues:

  • File Permission Errors: In cases where users face file permission issues, review and adjust the ownership and permissions of Magento 2 files and directories. Utilize commands such as chownand chmod to ensure proper access.
  • Database Connection Problems: If the installation fails to connect to the database, verify the correctness of your database credentials, ensuring they match the values specified during the setup.
  • PHP Version Compatibility: Ensure that your PHP version aligns with Magento 2 requirements. Incompatibility issues may arise if the PHP version is too old or too recent.
  • Composer Dependency Resolution: If encountering dependency resolution problems with Composer, run composer install to fetch the required dependencies or update Composer to the latest version. 

7. Post-Installation Tasks: Ensuring a Secure and Efficient Platform

Once your Magento 2 installation is successful, it's crucial to perform essential post-installation tasks to create a secure and efficient e-commerce platform. Follow these steps:

Secure Magento 2 Backend

  • Change the default admin URL to enhance security and protect against unauthorized access. This can be done through the Magento Admin.
  • Implement two-factor authentication for an additional layer of admin login security.
  • Regularly update admin passwords and limit admin access to necessary personnel only.

Configure Cron Jobs

Set up and configure cron jobs to automate various Magento tasks, such as reindexing, sending transactional emails, and other scheduled activities. Proper cron job configuration ensures timely execution of essential background processes.

Optimize Storefront for Search Engines 

  • Enable and configure Magento's SEO features to optimize your storefront for search engines.
  • Create a comprehensive sitemap to help search engines index your website efficiently.
  • Implement SEO-friendly URLs, meta tags, and descriptions for products and categories.

Review and Adjust System Configuration

Access the Magento Admin Panel and review general system configurations to align them with your business requirements. Adjust settings related to currency, language, shipping, and payment methods.

Enable Caching

Turn on caching to enhance the performance of your Magento 2 store. Utilize full-page caching mechanisms such as Varnish or Redis for improved speed.

Backup Your Magento 2 Instance

Regularly back up your Magento 2 files and database to safeguard against potential data loss. This is crucial before implementing major changes or updates.

8. Theme and Third-Party Integrations: Extending Functionality with Confidence 

As you delve into expanding the capabilities of your Magento 2 platform through themes and third-party extensions and modules, approach this process with caution. Here's a step-by-step guide to integrating third-party functionalities seamlessly:

Research and Select Reputable Themes and Extensions – Begin by researching and selecting extensions from reputable sources such as the Magento Marketplace. Check for user reviews, ratings, and the developer's credibility to ensure reliability. Consider using the Hyva theme as many robust and popular extensions are compatible with it.

Compatibility Check – Verify that the chosen themes or extensions are compatible with the specific version of Magento 2 you are using. Extensions designed for earlier versions may not function correctly or could cause conflicts. 

Review Documentation – Thoroughly review the documentation provided by the theme or extension developer. This documentation should include installation instructions, configuration options, and any potential conflicts with other extensions.

Test in a Staging Environment – Conduct thorough testing in a staging environment before deploying any theme or extension on your live site. This allows you to identify and address any issues without affecting the customer experience.

Back up Your Store – Before installation, create a complete backup of your Magento 2 store. This ensures that if something happens, you can quickly restore your site to its previous state.

Follow Best Practices – Adhere to best practices during the installation and configuration of third-party software. This includes following recommended settings, avoiding conflicts with existing modules, and maintaining a clean and organized codebase.

Monitor Performance – After integrating a theme or an extension, monitor your site's performance closely. Watch for any anomalies, such as increased load times or conflicts with other functionalities. Timely detection allows for swift resolution. Regularly check for updates and patches released by developers.

Conclusion: Empowering Users for Success

As Magento 2 installation methods continually evolve, always refer to the latest official documentation for accurate guidance. By following the guides step-by-step, you can ensure a smooth and successful implementation of your e-commerce platform.

→ Thinking of how to start with Magento? Talk to Amasty experts, or hire our developers to install Magento for you!

December 18, 2014
January 12, 2015
December 17, 2014
Comments
Olga
December 18, 2014
You're so fast! It was released just yesterday and you have so great report already. Thanks! It helps to find out what issues other people face with comparing to Magento team, which works with these tools every day. I just have small note. "chown -R admin:www-data /var/www/magento2" It means that you make user "admin" and group "www-data" owners of the files. "www-data" is not a user in this context, so it will work as far as your Apache user is in "www-data" group. It's little bit different than you describe in the explanation. In Magento documentation it's described as "Magento files and directories should be owned by the web server user". Maybe it can be rephrased somehow, the intent was to say that Apache user should have access to the files - "write" for some of them and "read" for all others. In your case Apache user owns the files because he is in "www-data" group, while it's not a real owner. And a question: you say "add write permissions for app/etc/ and vendor/". Could you, please, tell why did you need write permissions to "vendor" directory? Does installation fail, if "vendor" is not writable? Thanks
Reply
Andrey Tataranovich
December 19, 2014
Hi, and thanks for such a great comment! Regarding the user group question – you’re right, and thanks for pointing this out, I’m sure it will be helpful for all those people who’ll come and read the article. As for the second question – I couldn’t find an appropriate description of the directory to give write permissions for and took the information from one of the tickets here https://github.com/magento/magento2/issues/791#issuecomment-66433673 As the release is very fresh the documentation is still far from ideal and many descriptions need details – but the Magento guys pointed out that they will do their best on keeping the documentation up-to-date and precise in the future.
Richard Ireland
December 22, 2014
It's good to know Amasty are on top of Magento 2.0. Any official news on upgrade costs/procedures for existing Amasty customers? I am a big fan of your extensions, and was planning to recommend to a client they purchase some more in the next few weeks. However if they will need to purchase again after a migration to Magento 2.0 I may tell them hold off for a while.
Reply
Ksenia Dobreva
December 22, 2014
Hi, thanks for the question and the kind words! Unfortunately, it is still Developer Beta and we’re in the process of digging in Magento 2. It is still too early to speak about transition to Magento 2 for merchants. According to Elena Leonova from the Magento team (you can see more at https://amasty.com/blog/magento-2-news/) Magento 2 Merchant release is planned in the 4th quarter of 2015, so it’s at least a year before merchants will be able to use the new version to its fullest. Taking these facts into account, I doubt it's worth waiting a year or even more. (If your clients need new features right now or in the nearest future, don’t forget to send them the coupon code – it’s valid till December 28, details here https://amasty.com/blog/merry-christmas-20-off-on-all-extensions/). We updated our terms on Magento 2 support: https://amasty.com/terms-and-conditions.html «Updates for Magento of 2.X.X versions will not be provided for free even if the period of free updates and support will still be running. (…) Magento 2 will be a completely different solution in terms of architecture and will be incompatible with all extensions developed for Magento of 1.X.X versions, including the ones provided by Amasty. Due to this extensions for Magento 2 will need to be developed from scratch and will be treated as completely new extensions, not as new versions of extensions developed for Magento of 1.X.X versions.» Hope that helps. Will be happy to add anything in case you have any other questions.
Vincent ANDRE
January 20, 2016
is that still too early to talk about those upgrade costs and procedures?
yogi
December 25, 2014
have you installed the magento2 successfully? how i can run magento2 under ubuntu with index.php?
Reply
Ksenia Dobreva
December 29, 2014
Hi Yogi! Yes, we have installed Magento 2 successfully. Our instruction is adapted for Debian - so it should work with Ubuntu.
Dennis
April 28, 2015
I've tried it several times but when I want to install the sample-data-patch I always get this error: patch -p1 < magento2-require-sample-data.patch patching file composer.json Hunk #1 succeeded at 7 with fuzz 2. patch unexpectedly ends in middle of line Hunk #2 FAILED at 36. 1 out of 2 hunks FAILED -- saving rejects to file composer.json.rej I've been trying this with Magento 0.74.0-beta6 Any ideas?
Reply
Ksenia Dobreva
April 29, 2015
Hi Dennis, sorry for that, but looks like our instructions became irrelevant with time - things have changes since December. We will consider writing a new post.
Tony
May 13, 2015
Hi, I have installed magento version 2.0 successfully but when go to admin I don't click on any thing Can you help you ? Thank a lots! Regards Tony
Reply
Ksenia Dobreva
May 14, 2015
Hey Tony, thanks for asking. Unfortunately, we couldn't reproduce your bug... Please make sure that your server is equipped according to minimal Magento 2 requirements (pay special attention to the PHP version). If everything is okay with it, and the installation process went without mistakes, but the interface is not working, it can be a good idea to try to install Magento once more, and if the same problem appears, to submit a bug to Magento 2. Hope that helps.
Shawn Adams
August 6, 2015
If we use docker this image clearly help us install magento 2 https://hub.docker.com/r/ilampirai/docker-magento2/ share your thoughts ?
Reply
Ksenia Dobreva
August 12, 2015
Hi Shawn, I've consulted with my colleagues on that question, and they say for now this is more a playground than a way that could be used for staging/production usage. Hope that helps!
sam
August 20, 2015
can you help me how to install magento 2 under ubuntu machine. ? 1. In which directory i need to download composer ? Thanks
Reply
Ksenia Dobreva
August 20, 2015
Hi Sam, thanks for asking. sudo wget -O /usr/local/bin/composer http://getcomposer.org/composer.phar sudo chmod 0755 /usr/local/bin/composer Hope that helps!
Duc
October 29, 2015
Hi, I follow your link to install sample data : but when i finish and reload my page, it don't have anything, just same before i intall sample data. When i am wrong? Can you show me how can install sample data after install magento 2. I installed but nothing.
Reply
Ksenia Dobreva
October 29, 2015
Hey Duc, thanks for asking. Unfortunately, we can't help you right now because things change every time Magento rolls out a small update of version 2, and any instructions can't be stable at all. I think we will be ready to give some steady recommendations when the second version is officially out, but now any instructions usually expire very fast. I think now the best place to ask your question is this topic on Magento forums: https://community.magento.com/t5/Installing-Magento-2/bd-p/installing-magento-2 Hope that helps!
faran
November 25, 2015
Hi, I am facing the problem in ubantu that when I goto directory cd /var/www/magento2 and run composer install, it gives me the following error: [Composer\Downloader\TransportException] Invalid credentials for 'https://repo.magento.com/archives/magento/composer /magento-composer-1.0.2.0.zip', aborting. Kindly let me know which credentials to use and how to resolve this. Will be really helpfull. Thanks
Reply
Ksenia Dobreva
November 25, 2015
Hi Faran, thanks for asking. It's a pretty old instruction, now you can download Magento as an archive from here https://www.magentocommerce.com/download and install it much easier. Our instruction was valid before the official release. Hope that helps!
Jaime
November 30, 2015
I have followed instructions in your page, however, installation always get stuck and different percentages, for example, I have just tried again, it was stuck at 71%. I am using Web Interface. I tried composer and this was shown: Problem 1 - magento/framework 100.0.2 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system. - magento/framework 100.0.2 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system. - Installation request for magento/framework 100.0.2 -> satisfiable by magento/framework[100.0.2]. I tried even this command: php -d xdebug.max_nesting_level=500 -f bin/magento setup:install --base_url=http://commerce.gestionx.cl/ --backend_frontname=admin --db_host=localhost --db_name=gestionx_commerce --db_user=gestionx_comuser --db_pass=C0MM3rc3Db --admin_firstname=Administrador --admin_lastname=Local [email protected] --admin_username=administrador --admin_password=4dm1nM4g3nt0 --language=es_CL --currency=CLP --timezone=America/Santiago But this does simply nothing. Command returns immediately with no error. I cannot use any installation method. Can you please tell me what might be happening here? Thanks Jaime
Reply
Ksenia Dobreva
November 30, 2015
Hey Jaime, this instruction is a bit outdated, it was valid before Magento 2 general availability release. Try installing php5-mcrypt and installing the package from Magento website https://www.magentocommerce.com/download Hope that helps!
Paolo
March 16, 2021
The article is unreadable! You must somehow structure the information in the article!
Reply
Vladimir Derachits
March 16, 2021
Sorry for having a problem with this article. It's quite outdated and we'll see what we can do to avoid confusion about this article in the future.
Leave your comment

Your email address will not be published

This blog was created with Amasty Blog Pro

This blog was created with Amasty Blog Pro