Deploying a Magento 2 Project with DeployHQ
This guide will walk you through creating a new Magento 2 project and deploying it using DeployHQ.
Creating a New Magento 2 Project
Before setting up DeployHQ, you'll need to create your Magento 2 project. Follow these steps:
Ensure you have Composer installed on your local machine.
Open a terminal and navigate to the directory where you want to create your project.
Run the following command to create a new Magento 2 project:
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition <install-directory-name>
Replace <install-directory-name>
with your desired project name.
When prompted, enter your Magento Marketplace authentication keys. If you don't have these, you can get them from your Magento Marketplace account.
Once the installation is complete, navigate into your project directory:
cd <install-directory-name>
- Initialize a Git repository and commit your files:
git init
git add .
git commit -m "Initial Magento 2 project setup"
- Create a new repository on GitHub or your preferred Git hosting service, and push your local repository to it.
Setting Up DeployHQ
DeployHQ is a service that simplifies deploying applications to your servers. You can define configurations, custom commands, and automate deployments on pushing code.
Creating a Project in DeployHQ
- Sign up for a DeployHQ account if you haven't already.
- Log in to DeployHQ.
- Go to Projects > New Project.
- Follow the wizard and choose the repository where you pushed your Magento 2 project.
- If you encounter issues connecting, refer to DeployHQ's support page.
Configuring a Server
Now, tell DeployHQ where your Magento 2 application will reside:
- Go to Servers > New Server.
- Choose a memorable name and select SSH as the protocol.
- Enter your server's IP address in the Hostname field.
- Set the Username to a dedicated deployment user (e.g.,
deployhq
). - Check the "Use SSH key rather than password for authentication?" box.
- Set the Deployment Path to where your Magento 2 application will reside (e.g.,
/var/www/your-magento-project
). - Enable "Perform zero-downtime deployments on this server."
- Enter "production" in the Environment field and enable automatic deploy.
- Click Save.
Setting Up the Deployment User
On your server, create a dedicated user for deployments:
sudo adduser deployhq
sudo usermod -a -G www-data deployhq
Set up SSH access for this user:
su - deployhq
mkdir ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
Paste DeployHQ's public key into this file, save and exit. Then set the correct permissions:
chmod 600 ~/.ssh/authorized_keys
Config Files (Optional)
To manage sensitive configuration files:
- Go to Config Files in DeployHQ and click New Config File.
- Copy the contents of your
env.php
or other configuration files into the config file content field.
Setting Up SSH Commands
Configure commands to execute on the server after deployment:
- Go to SSH Commands and click New SSH Command.
- Add the following commands in order:
a. Set Permissions:
cd %path% && find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} + && find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
b. Update Composer Dependencies:
cd %path% && composer install --no-dev --optimize-autoloader
c. Clear Cache:
cd %path% && php bin/magento cache:flush
d. Update Database Schema:
cd %path% && php bin/magento setup:upgrade
e. Compile DI and Generate Static Content:
cd %path% && php bin/magento setup:di:compile && php bin/magento setup:static-content:deploy -f
f. Set Production Mode:
cd %path% && php bin/magento deploy:mode:set production
Deploying Your Project
You're now ready to deploy:
- Click "Deploy Project" in the DeployHQ header.
- Ensure the correct server and revisions are selected.
- Click "Deploy" to start the deployment process.
Congratulations! You've now automated your Magento 2 project deployments with DeployHQ.