How to Setup Magento Server on Azure/AWS/GCP
To setup and install Magento 2 open source server on any of the cloud platforms, the recommended way is to deploy using the server image available in the marketplace. It comes pre-installed with Apache Web Server, MySQL Server, all the required PHP modules and ElasticSearch. View the links below to deploy to your cloud environment:
Magento Cloud Server
Getting Started
Â
Once your Magento server has been deployed, the following links explain how to connect to a Linux VM:
Â
- How to connect to a Linux VM on Azure
- How to connect to a Linux VM on AWS
- How to connect to a Linux VM on GCP
Â
Once connected and logged in, you’re ready to start configuring Magento.
Setting up Magento Community Open Source 2.4 Server
This solution is built using Apache Web Server, MySQL 8 Server and PHP 7.4.
1.) Create a MySQL database for Magento installation
We need to run the following commands to setup a MySQL database using MariaDB Server.Â
Â
First we need to set the root mysql password (currently password is blank), run the following command and follow the onscreen instructions to set a new root password. Press enter when it asks to provide a password.
sudo mysql_secure_installation
Next we run the following command to create a new database called magento2 with a new user called magentip with password yourpassword that will be used by Magento.Â
sudo mysql
CREATE DATABASE magento2;
CREATE USER 'magentip'@'localhost' IDENTIFIED BY 'yourpassword';
ALTER USER 'magentip'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON *.* TO 'magentip'@'localhost' WITH GRANT OPTION;
exit;
2.) Create Magento Access Key
In order to setup magento 2.4 you will require access keys. Sign up for a free account on the following link and create a new access key. You will receive a private and public key that we will use to setup Magento
Â
3.) Configure Apache2 Virtual Host for Magento Site
Â
We need to configure the default Apache host config for our Magento site.
Â
Open the 000-default.conf file:
Â
sudo vim /etc/apache2/sites-available/000-default.conf
Â
Copy and paste the following into your 000-default.conf file:
Â
DocumentRoot /var/www/html/magento2/ <Directory /var/www/html/magento2/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
Edit config using VIM editor
Â
Press Shift + I to insert the text into the 000-default.conf file using the VIM editor. To save and exit, press ESC key and type :wq!
Enable Apache Rewrite
Â
Once you have saved the 000-default.conf and exited back to terminal, we need to enable the Apache rewrite module by running the following command:
Â
sudo a2enmod rewrite
Â
Restart Apache Service
Â
Now restart Apache by running the following command:
Â
sudo systemctl restart apache2
Update PHP.ini File
Â
Next you need to make sure the following entries are configured within your php.ini file. Run the following command to open your php.ini file:
sudo nano /etc/php/7.4/apache2/php.ini
Search through the file and making sure the following are configured:
file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago
Then restart Apache service:
Â
sudo systemctl restart apache2
4.) Install Magento into Apache vhost directory
Â
Now we need to run through the Magento installation. We will be using Composer and the Magento access keys we created earlier.
Â
Your access keys will be used as follows:
Â
Username: Your public key
Password: Your private key
Â
Step 1 – Change terminal session path
Â
Make sure the current directory your terminal session is in magento web root directory:
Â
cd /var/www/html
Â
Step 2 – Download Magento2 Open Source files
Â
Run the following command. It will take some time to download.
sudo composer create-project --repository=https://repo.magento.com/ magento/project-community-edition=2.4.3 magento2
You will be prompted for a username and password, this is where you use your Magento access keys as follows:
Â
Username: Your public key
Password: Your private key
Â
Â
Step 3 – Set Magento folder permissions
Â
You must set read-write permissions for the web server group before you install the Magento software. This is necessary so that the command line can write files to the Magento file system.
Â
sudo chown -R www-data:www-data /var/www/html/magento2/
sudo chmod -R 755 /var/www/html/magento2/
cd /var/www/html/magento2
sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
sudo chown -R :www-data .Â
sudo chmod u+x bin/magento
Â
Step 4 – Restart Elasticsearch
Â
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch
Â
Step 5 – Install Magento
Â
Make sure you’re currently in your Magento directory
Â
cd /var/www/html/magento2
Â
Run the following command and amend the details to match your URL, database details, passwords, etc
sudo bin/magento setup:install \ --base-url=http://mywebsite.com \ --db-host=localhost \ --db-name=magento2 \ --db-user=magentip \ --db-password=youmysqlpassword \ --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
Step 5 – Setup Cron Jobs
Â
Magento is using cron jobs to automate important operations. Lets setup the cron jobs.
sudo bin/magento cron:install
Magento Admin Login
Â
Once the install is complete, you will see the following, make a note of the admin URL as this is the unique URL you will use to login to the Admin portal:
Â
In my example, my admin login would be as follows:
Â
http://sitename.com/admin_l6ach9
Â
You should now be able to access Magento, view your homepage using your URL – http://sitename.com or the servers IP
Â
Magento Login – Two Factor Authorization
Â
The first time your login to the admin section, you may get an error about configuring two-factor authentication:
Â
This error happened because since Magento 2.4, Two-Factor Authorization is enabled by default.
Â
There are 2 ways to fix this:
Â
- Method 1:Â Disable Two-Factor Authorization
- Method 2:Â Install and enable Two-Factor Authorization
Method 1 – Disable Two-Factor Authorization:
Â
If you don’t need Two-Factor Authorization, run this command within your magento directory to disable it
Method 2 – Enable Two-Factor Authorization
Â
If you wish to use two-factor authorization, follow these step to enable this feature.
Â
First, run this command within your Magento directory to force Magento 2.4 to use Google as the Two-factor authorization provider
Â
sudo bin/magento config:set twofactorauth/general/force_providers google
Â
Next, generate a Base32-encoded string for the OTP secret key. You can use some online tool for this. For example: https://emn178.github.io/online-tools/base32_encode.html
Â
Input a normal password like: mypassword and you will receive a Base32-encoded string like this: NV4XAYLTON3W64TE
Â
Â
Next, we will add the secret encoded OTP key to Google Authenticator with this command
Â
sudo bin/magento security:tfa:google:set-secret admin <code>NV4XAYLTON3W64TE</code>
Â
Now open Google Authenticator App on your mobile: https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2&hl=en&gl=US and add new key: NV4XAYLTON3W64TE
Â
After you add the new key, you will see Authenticator code in your application once you login to Magento 2 dashboard
Â
Enter the code you you receive in authenticator app to login to admin dashboard
Â
Apply Latest Magento Security Patches
As of Feb 12 2022, for Magento versions 2.4.3-p1 and earlier versions a security patch should be applied as per APSB22-12 (CVE-2022-24086, CVE-2022-24087)
Â
First create a folder within the Magneto2 root directory:Â /var/www/html/magento2
sudo mkdir hotfix
cd hotfix
Download the 2 required patches to your new hotfix directory:
sudo wget https://github.com/magento/knowledge-base/raw/main/src/troubleshooting/known-issues-patches-attached/assets/MDVA-43395_EE_2.4.3-p1_COMPOSER_v1.patch.zip
sudo wget https://github.com/magento/knowledge-base/raw/main/src/troubleshooting/known-issues-patches-attached/assets/MDVA-43443_EE_2.4.2-p2_COMPOSER_v1.patch.zip
Now extract to contents of these zip files and copy the contents back to the Magento root directory:
sudo unzip MDVA-43395_EE_2.4.3-p1_COMPOSER_v1.patch.zip
sudo unzip MDVA-43443_EE_2.4.2-p2_COMPOSER_v1.patch.zip
sudo cp MDVA-43395_EE_2.4.3-p1_COMPOSER_v1.patch /var/www/html/magento2/
sudo cp MDVA-43443_EE_2.4.2-p2_COMPOSER_v1.patch /var/www/html/magento2/
cd ..
Within the magento2 root directory run the following commands to apply the patches:
sudo patch -p1 < MDVA-43395_EE_2.4.3-p1_COMPOSER_v1.patch
sudo patch -p1 < MDVA-43443_EE_2.4.2-p2_COMPOSER_v1.patch
For the changes to be reflected, refresh the cache in the Admin under System > Cache Management.
Magento Firewall Ports
Â
Magento uses the following firewall ports:
Â
- TCP Port 80 – http
- TCP Port 443 -https
- TCP Port 9200 – ElasticSearch
Â
To setup AWS firewall rules refer to – AWS Security Groups
To setup Azure firewall rules refer to – Azure Network Security Groups
To setup Google GCP firewall rules refer to – Creating GCP Firewalls
Support / Documentation
Â
Its recommended to perform the following post setup configurations for Magento
Â
https://devdocs.magento.com/guides/v2.4/install-gde/install/post-install-config.html
Â
Further documentation and support can be found on the following link:
Â
Â
Related Posts:
- Magento Server Backup: How to Backup Magento Server Data
- Magento Server Migration: Migrate Magento Store to a New Server
- Magento Server Security: How to Secure Your Magento Server
- Magento Server Monitoring: Monitor Magento for Best Performance
- Magento Open Source vs Magento Commerce (Compare the Differences)
- How to Setup hMailServer Email Server (SMTP Server) in Azure/AWS/GCP
/home/713724.cloudwaysapps.com/xrsuqftxbe/public_html/magento2$ sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
find: ‘var’: No such file or directory
find: ‘generated’: No such file or directory
find: ‘vendor’: No such file or directory
find: ‘pub/static’: No such file or directory
find: ‘pub/media’: No such file or directory
find: ‘app/etc’: No such file or directory
Hi Peter,
Thanks for your feedback, did Step 2 – Download Magento2 Open Source files – download the Magento files successfully ?
Is there any files in /var/www/html ?
HI Andrew, I reinstalled as per instructions,
Another question before i get to that error, Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? the only option is yes, to continue
Do you want to store credentials for repo.magento.com in /root/.config/composer/auth.json ? [Yn] y
Installing magento/project-community-edition (2.4.2)
– Downloading magento/project-community-edition (2.4.2)
– Installing magento/project-community-edition (2.4.2): Extracting archive
Created project in /home/713724.cloudwaysapps.com/xrsuqftxbe/public_html/magento2
Loading composer repositories with package information
Updating dependencies
Killed
? should it say killed ?
Hi Peter, after doing a Google of the ‘Killed’ error its to do with composer running out of memory. Try the following suggestions on this thread – https://magento.stackexchange.com/questions/238225/magento-2-composer-runs-out-of-memory
Also what is the size of the VM you are currently using ?
thank you