Magento 2 is a powerful and widely used e-commerce platform that empowers businesses to create and manage online stores with utmost flexibility and efficiency. It serves as a comprehensive solution for companies seeking to establish a robust and customizable online presence. Magento 2 builds upon the success of its predecessor, Magento 1, offering enhanced performance, improved scalability, and an array of advanced features.
Developing a Magento 2 store requires a reliable and efficient development environment. Docker and Warden together provide a powerful combination for creating a consistent and isolated environment for Magento development. Docker enables containerization, while Warden orchestrates the docker containers to build the essential infrastructure on any desired environment.
This technical blog will guide you through the process of setting up a Magento 2 development environment using Docker and Warden, allowing you to streamline your development workflow and focus on building outstanding e-commerce experiences.
Understanding Docker and Warden
Docker is an open-source platform that automates the deployment and management of applications using containerization. It allows developers to package applications and their dependencies into portable containers, providing a consistent environment across different systems. Docker containers are lightweight, isolated, and share the host system's kernel, making them highly efficient and scalable.
Warden extends Docker by providing a set of pre-configured Docker containers tailored for Magento development. Warden containers are optimized for Magento's requirements and come with essential tools and services pre-installed, making it easy to set up a Magento development environment.
The following are the procedures for configuring a Magento 2 development environment using Docker and Warden:
Prerequisites
The following prerequisites should be installed before setting up your Magento environment:
- Install Docker in your system. Docker for Linux
- Install docker-composer. Docker-composer for Linux
- Install Warden
Installing Magento 2
Step 1: Create a New Directory
Begin the process by creating a brand new directory on your host machine, and selecting a location that suits your preferences and organizational structure. This directory will serve as the foundation for your Magento 2 e-commerce project, and it's essential to choose a descriptive and memorable name for it. For this guide, we'll name it "exampleproject" to illustrate the steps effectively.
mkdir -p ~/Sites/exampleproject
cd ~/Sites/exampleproject
Step 2: Initialize Environment Configuration
With the new directory in place, it's time to initiate the environment configuration, a critical step that will lay the groundwork for your project's seamless integration with Warden and Docker. To accomplish this, you'll create a file named ".env" (called environment file) containing the necessary configuration settings specific to your Magento 2 project, enabling a smooth and cohesive interaction between Warden, Docker, and your e-commerce application.
warden env-init exampleproject magento2
Step 3: Sign an SSL Certificate
As security remains paramount for online businesses, it becomes imperative to establish secure connections for your project. To achieve this, you'll need to sign an SSL certificate that aligns perfectly with your chosen domain. The information you input during this process should match the value of TRAEFIK_DOMAIN, which you previously specified in the ".env" file, ensuring a seamless and secure experience for your customers.
warden sign-certificate exampleproject.test
Step 4: Start the Project Environment
Having completed the initial environment setup, it's now time to set your project environment into motion. By initiating this environment through Warden, you will create an environment where your Magento 2 application can run optimally, with all the necessary components and dependencies precisely orchestrated for smooth operation.
warden env up
Step 5: Configure Global Magento Marketplace Credentials
To ensure your Magento 2 project can access essential resources and components available in the Magento Marketplace, it is vital to configure global Magento Marketplace credentials within your project environment's shell. These credentials serve as authentication keys, granting your application the necessary access permissions to utilize Magento's extensive resources for your e-commerce venture.
composer global config http-basic.repo.magento.com <username> <password>
Step 6: Initialize Project Source Files
Now that your environment is equipped with the essential credentials, it's time to initialize your project's source files, effectively setting the wheels in motion for your Magento 2 ecommerce store. Utilizing the powerful capabilities of composer create-project, your project's source files will be meticulously generated and placed within a temporary directory named "/tmp/exampleproject" before being seamlessly moved into their rightful position, "/var/www/html/," where your store will come to life.
META_PACKAGE=magento/project-community-edition
META_VERSION=2.4.x
composer create-project --repository-url=https://repo.magento.com/
"${META_PACKAGE}" /tmp/exampleproject "${META_VERSION}"
rsync -a /tmp/exampleproject/ /var/www/html/
rm -rf /tmp/exampleproject/
Step 7: Install the Application
With the foundation firmly established, you're now ready to install the heart and soul of your Magento 2 ecommerce project. By running the "bin/magento setup:install" command, you will initiate a comprehensive setup process, during which you will be prompted to provide crucial configuration options, effectively personalizing your store and tailoring it to your specific requirements.
bin/magento setup:install \
--backend-frontname=backend \
--amqp-host=rabbitmq \
--amqp-port=5672 \
--amqp-user=guest \
--amqp-password=guest \
--db-host=db \
--db-name=magento \
--db-user=magento \
--db-password=magento \
--search-engine=opensearch \
--opensearch-host=opensearch \
--opensearch-port=9200 \
--opensearch-index-prefix=magento2 \
--opensearch-enable-auth=0 \
--opensearch-timeout=15 \
--http-cache-hosts=varnish:80 \
--session-save=redis \
--session-save-redis-host=redis \
--session-save-redis-port=6379 \
--session-save-redis-db=2 \
--session-save-redis-max-concurrency=20 \
--cache-backend=redis \
--cache-backend-redis-server=redis \
--cache-backend-redis-db=0 \
--cache-backend-redis-port=6379 \
--page-cache=redis \
--page-cache-redis-server=redis \
--page-cache-redis-db=1 \
--page-cache-redis-port=6379
Step 8: Lock Down Environment URLs
To ensure that your Magento 2 store operates with utmost security and efficiency, you need to lock down the base URLs within the system configuration. This entails setting up secure connections that will protect sensitive information and create a seamless user experience. Execute the following commands to accomplish this:
bin/magento config:set --lock-env web/unsecure/base_url
"https://${TRAEFIK_SUBDOMAIN}.${TRAEFIK_DOMAIN}/"
bin/magento config:set --lock-env web/secure/base_url
"https://${TRAEFIK_SUBDOMAIN}.${TRAEFIK_DOMAIN}/"
bin/magento config:set --lock-env web/secure/offloader_header
X-Forwarded-Proto
bin/magento config:set --lock-env web/secure/use_in_frontend 1
bin/magento config:set --lock-env web/secure/use_in_adminhtml 1
bin/magento config:set --lock-env web/seo/use_rewrites 1
Step 9: Optimize Caching and Indexing
As performance plays a vital role in delivering a delightful shopping experience to your customers, optimizing caching and indexing settings becomes a crucial step in enhancing your Magento 2 store's efficiency. By executing the following commands, you'll ensure that your store operates at peak performance, significantly reducing load times and providing a seamless shopping journey for your valued customers.
bin/magento config:set --lock-env system/full_page_cache/caching_application 2
bin/magento config:set --lock-env system/full_page_cache/ttl 604800
bin/magento config:set --lock-env catalog/search/enable_eav_indexer 1
bin/magento config:set --lock-env dev/static/sign 0
bin/magento deploy:mode:set -s developer
bin/magento cache:disable block_html full_page
bin/magento indexer:reindex
bin/magento cache:flush
Step 10: Generate an Admin User
In preparation for managing your Magento 2 store effectively, you'll need to create an admin user account. This user account will grant you privileged access to the store's backend, enabling you to customize, manage, and oversee every aspect of your e-commerce venture.
php bin/magento admin:user:create
By following these comprehensive and detailed steps, you'll successfully install Magento 2 and lay the foundation for a thriving and successful e-commerce project.
Launch the application in browser:
Apart from the main steps mentioned in the guide, there are several other useful commands that can come in handy during your Magento 2 installation and management journey. Familiarizing yourself with these additional commands will allow you to have more control over your environment and streamline various tasks.
Stopping a Running Environment:
If you need to temporarily halt your running environment, perhaps for maintenance purposes or to free up system resources, you can execute the following command:
warden env stop
Starting a Stopped Environment:
When you're ready to resume operations after stopping the environment, simply use the following command to start it back up:
warden env start
Importing a Database:
At times, you may need to import a database dump (e.g., "dump.sql.gz") into your Magento 2 project. The command below will help you accomplish this task efficiently:
pv /path/to/dump.sql.gz | gunzip -c | warden db import
Console for Imported Database:
In certain situations, you might need to access the imported database directly from the console. The following command will facilitate this action:
warden db connect -A
Connecting to Redis:
Redis is an important component in your Magento 2 environment, and sometimes, you may need to connect to it for management or troubleshooting purposes. This command will establish a connection:
warden redis
Flushing Redis Completely:
If you find it necessary to completely flush Redis, you can execute the following command:
warden redis flushall
Removing Volumes Completely:
If you decide to start fresh or clean up the environment by removing all volumes associated with your Magento 2 project, use the command below:
warden env down -v
By incorporating these additional commands into your Magento 2 management arsenal, you'll gain more flexibility and control over your e-commerce environment. Whether it's pausing or resuming your environment or importing databases these commands will prove invaluable in streamlining your development and maintenance processes.
Run warden help and warden env -h for more details and command information.
In conclusion, setting up a Magento 2 development environment using Docker and Warden provides a reliable, efficient, and consistent platform for building outstanding e-commerce experiences. With Docker's containerization and Warden's infrastructure management capabilities, developers can easily replicate, version control, and scale their environments while ensuring time and resource efficiency. Additionally, the use of Docker containers enhances security by isolating applications and dependencies. By leveraging these tools, developers can streamline their workflows, collaborate effectively, and focus on developing exceptional Magento 2 stores.