DEV Community

Subham Nandi
Subham Nandi

Posted on

Assignment

Here’s a step-by-step guide to launching an EC2 instance of Debian 11 on AWS and installing the required components: PHP 8.1, MySQL 8, NGINX, and Elasticsearch.

Step 1: Launch a Debian 11 Instance on AWS

  1. Log into your AWS account.
  2. Navigate to the EC2 Dashboard.
  3. Click on Launch Instance.
  4. Choose an Amazon Machine Image (AMI):
    • Search for Debian 11 in the community AMIs section.
    • Select Debian 11 (Bullseye).
  5. Choose an instance type (e.g., t2.medium for better performance).
  6. Configure your instance settings (like VPC, subnets, and security groups).
  7. Add storage (at least 20 GB).
  8. Review and launch the instance.

After launching, access the instance via SSH:

ssh -i your-key.pem admin@your-instance-public-ip
Enter fullscreen mode Exit fullscreen mode

Step 2: Update the System

Before installing any software, ensure your system is up to date:

sudo apt update
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 3: Install NGINX

  1. Install NGINX:
   sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode
  1. Start and enable NGINX:
   sudo systemctl start nginx
   sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

Step 4: Install PHP 8.1

  1. Install required dependencies:
   sudo apt install software-properties-common -y
Enter fullscreen mode Exit fullscreen mode
  1. Add the PHP repository for version 8.1:
   sudo add-apt-repository ppa:ondrej/php
   sudo apt update
Enter fullscreen mode Exit fullscreen mode
  1. Install PHP 8.1 and required modules:
   sudo apt install php8.1 php8.1-fpm php8.1-mysql php8.1-cli php8.1-curl php8.1-mbstring php8.1-xml php8.1-zip -y
Enter fullscreen mode Exit fullscreen mode
  1. Start and enable PHP-FPM:
   sudo systemctl start php8.1-fpm
   sudo systemctl enable php8.1-fpm
Enter fullscreen mode Exit fullscreen mode

Step 5: Install MySQL 8

  1. Download and install the MySQL APT repository package:
   wget https://dev.mysql.com/get/mysql-apt-config_0.8.23-1_all.deb
   sudo dpkg -i mysql-apt-config_0.8.23-1_all.deb
Enter fullscreen mode Exit fullscreen mode
  • When prompted, select MySQL 8.0 and press Enter.
  1. Update the package list and install MySQL 8:
   sudo apt update
   sudo apt install mysql-server -y
Enter fullscreen mode Exit fullscreen mode
  1. Start and enable MySQL:
   sudo systemctl start mysql
   sudo systemctl enable mysql
Enter fullscreen mode Exit fullscreen mode
  1. Secure the MySQL installation:
   sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode
  • Set a strong password for the root user, remove anonymous users, disable remote root login, and remove the test database.

Step 6: Install Elasticsearch

  1. Install the Elasticsearch GPG key and repository:
   wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
   sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
Enter fullscreen mode Exit fullscreen mode
  1. Update the package list and install Elasticsearch:
   sudo apt update
   sudo apt install elasticsearch -y
Enter fullscreen mode Exit fullscreen mode
  1. Configure Elasticsearch: Open the Elasticsearch configuration file:
   sudo nano /etc/elasticsearch/elasticsearch.yml
Enter fullscreen mode Exit fullscreen mode

Modify the following settings:

   cluster.name: my-cluster
   node.name: my-node
   network.host: 0.0.0.0  # to bind Elasticsearch to public IP (optional)
Enter fullscreen mode Exit fullscreen mode
  1. Start and enable Elasticsearch:
   sudo systemctl start elasticsearch
   sudo systemctl enable elasticsearch
Enter fullscreen mode Exit fullscreen mode
  1. Test that Elasticsearch is running:
   curl -X GET "localhost:9200/"
Enter fullscreen mode Exit fullscreen mode

You should see a JSON response with details about the Elasticsearch node.

Step 7: Configure NGINX for PHP 8.1

  1. Create an NGINX server block for PHP:
   sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode
  1. Update the configuration file as follows:
   server {
       listen 80;
       server_name your-domain.com;

       root /var/www/html;
       index index.php index.html index.htm;

       location / {
           try_files $uri $uri/ =404;
       }

       location ~ \.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
       }

       location ~ /\.ht {
           deny all;
       }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Test the NGINX configuration:
   sudo nginx -t
Enter fullscreen mode Exit fullscreen mode
  1. Reload NGINX to apply changes:
   sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Step 8: Test PHP and MySQL

  1. Create a test PHP file:
   sudo nano /var/www/html/info.php
Enter fullscreen mode Exit fullscreen mode

Add the following content:

   <?php
   phpinfo();
   ?>
Enter fullscreen mode Exit fullscreen mode
  1. Visit http://your-public-ip/info.php to see the PHP information page.

This setup gives you a working Debian 11 instance with PHP 8.1, MySQL 8, NGINX, and Elasticsearch. Let me know if you need further configurations or specific setup steps!


Here's a step-by-step guide on installing Magento 2 with Sample Data and configuring Elasticsearch using Composer on a domain like test.mgt.com. We will assume you're using a Debian 11 server, and you've already set up PHP 8.1, MySQL 8, NGINX, and Elasticsearch as described in the previous instructions.

Step 1: Set Up the Domain with Hosts Entry

Before you begin the installation, add an entry in your /etc/hosts file for local development.

  1. Open the /etc/hosts file:
   sudo nano /etc/hosts
Enter fullscreen mode Exit fullscreen mode
  1. Add the following entry to associate test.mgt.com with localhost:
   127.0.0.1    test.mgt.com
Enter fullscreen mode Exit fullscreen mode
  1. Save and exit.

Step 2: Set Up Web Root and Permissions

  1. Create a directory for Magento inside the web root:
   sudo mkdir -p /var/www/test.mgt.com
Enter fullscreen mode Exit fullscreen mode
  1. Change the ownership of the directory to your web server user (in this case, www-data for NGINX):
   sudo chown -R www-data:www-data /var/www/test.mgt.com
   sudo chmod -R 755 /var/www/test.mgt.com
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Composer

If you haven't installed Composer yet, install it by running:

sudo apt install curl -y
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Enter fullscreen mode Exit fullscreen mode

Step 4: Install Magento 2 via Composer

  1. Navigate to the Magento directory:
   cd /var/www/test.mgt.com
Enter fullscreen mode Exit fullscreen mode
  1. Install Magento 2 using Composer:
   composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.6 .
Enter fullscreen mode Exit fullscreen mode
  • During the installation process, you will be prompted to enter your Magento Marketplace Authentication keys. If you don't have these keys, you can obtain them from your Magento Marketplace account.

Step 5: Install Sample Data (Optional)

  1. To install Magento with Sample Data, run the following command:
   php bin/magento sampledata:deploy
Enter fullscreen mode Exit fullscreen mode
  1. Update the Magento setup after installing the sample data:
   php bin/magento setup:upgrade
Enter fullscreen mode Exit fullscreen mode

Step 6: Configure NGINX for Magento

  1. Create a new NGINX server block for Magento:
   sudo nano /etc/nginx/sites-available/test.mgt.com
Enter fullscreen mode Exit fullscreen mode
  1. Add the following configuration to the file:
   server {
       listen 80;
       server_name test.mgt.com;
       set $MAGE_ROOT /var/www/test.mgt.com;
       include /var/www/test.mgt.com/nginx.conf.sample;

       location ~ \.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
       }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Enable the configuration:
   sudo ln -s /etc/nginx/sites-available/test.mgt.com /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode
  1. Test the configuration:
   sudo nginx -t
Enter fullscreen mode Exit fullscreen mode
  1. Reload NGINX:
   sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Step 7: Configure Elasticsearch for Magento

Magento 2.4.x requires Elasticsearch as the search engine. Follow these steps to configure it:

  1. Verify that Elasticsearch is running:
   curl -X GET "localhost:9200/"
Enter fullscreen mode Exit fullscreen mode
  1. Ensure that Magento connects to Elasticsearch using the correct host and port (localhost:9200 by default).

Step 8: Magento Installation via CLI

  1. Run the following command to install Magento 2:
   php bin/magento setup:install \
   --base-url="http://test.mgt.com/" \
   --db-host="localhost" \
   --db-name="magento" \
   --db-user="magentouser" \
   --db-password="yourpassword" \
   --admin-firstname="admin" \
   --admin-lastname="user" \
   --admin-email="admin@test.mgt.com" \
   --admin-user="admin" \
   --admin-password="Admin123!" \
   --backend-frontname="admin" \
   --language="en_US" \
   --currency="USD" \
   --timezone="America/Chicago" \
   --use-rewrites="1" \
   --search-engine="elasticsearch7" \
   --elasticsearch-host="127.0.0.1" \
   --elasticsearch-port="9200"
Enter fullscreen mode Exit fullscreen mode
  1. Once the installation is complete, set the permissions:
   sudo chown -R www-data:www-data /var/www/test.mgt.com
   sudo chmod -R 755 /var/www/test.mgt.com
Enter fullscreen mode Exit fullscreen mode

Step 9: Finalize Setup

  1. Disable Magento Maintenance Mode (if it's enabled):
   php bin/magento maintenance:disable
Enter fullscreen mode Exit fullscreen mode
  1. Reindex the Magento store:
   php bin/magento indexer:reindex
Enter fullscreen mode Exit fullscreen mode
  1. Deploy Static Content:
   php bin/magento setup:static-content:deploy -f
Enter fullscreen mode Exit fullscreen mode
  1. Clear Cache:
   php bin/magento cache:clean
Enter fullscreen mode Exit fullscreen mode

Step 10: Access Magento in the Browser

Now, you can access Magento at http://test.mgt.com and the admin panel at http://test.mgt.com/admin.


With these steps, you have successfully installed Magento 2 with Sample Data, Elasticsearch, and NGINX on Debian 11 using a domain like test.mgt.com. Let me know if you run into any issues!


Here's a step-by-step guide to installing Redis and configuring Magento 2 to store cache files and sessions in Redis instead of the file system.

Step 1: Install Redis on Debian 11

  1. Update your package list and install Redis:
   sudo apt update
   sudo apt install redis-server -y
Enter fullscreen mode Exit fullscreen mode
  1. Once installed, check if Redis is running:
   sudo systemctl status redis
Enter fullscreen mode Exit fullscreen mode

You should see Redis in an active/running state.

  1. Enable Redis to start on boot:
   sudo systemctl enable redis-server
Enter fullscreen mode Exit fullscreen mode
  1. Verify Redis is working by connecting to the Redis CLI:
   redis-cli
Enter fullscreen mode Exit fullscreen mode

Then run the ping command, and you should get a response like this:

   PONG
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure Redis for Magento 2

Before configuring Magento, modify the Redis configuration for performance and security:

  1. Open the Redis configuration file:
   sudo nano /etc/redis/redis.conf
Enter fullscreen mode Exit fullscreen mode
  1. Find and modify the following lines as needed:

    • Set the max memory usage (optional): If you want to limit the memory usage of Redis:
     maxmemory 256mb
     maxmemory-policy allkeys-lru
    
  2. Restart Redis to apply the changes:

   sudo systemctl restart redis-server
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure Magento 2 to Use Redis for Cache and Sessions

Magento can be configured to use Redis for both caching and session management by modifying the env.php file.

  1. Open the Magento configuration file (env.php):
   sudo nano /var/www/test.mgt.com/app/etc/env.php
Enter fullscreen mode Exit fullscreen mode
  1. Add the following configurations to enable Redis for session storage and cache storage:

Redis for Session Storage:

   'session' => [
       'save' => 'redis',
       'redis' => [
           'host' => '127.0.0.1',     // Redis server hostname
           'port' => '6379',          // Redis server port
           'password' => '',          // Redis password (empty if none)
           'timeout' => '2.5',        // Connection timeout
           'persistent_identifier' => '', // Persistent connection ID (optional)
           'database' => '2',         // Redis database number for sessions (use a dedicated one)
           'compression_threshold' => '2048', // Minimum size for compression
           'compression_library' => 'gzip',   // Compression algorithm
           'log_level' => '1',        // Log errors (0 to disable, 1 for errors only)
           'max_concurrency' => '6',  // Maximum concurrent Redis connections (sessions)
           'break_after_frontend' => '5', // Retry timeout after failure
           'fail_after' => '10',      // Connection fail time (seconds)
           'first_lifetime' => '600', // Session lifetime (seconds)
           'bot_first_lifetime' => '60',
           'bot_lifetime' => '7200',  // Lifetime for bots (seconds)
           'disable_locking' => false,// Disable session locking
           'min_lifetime' => '60',    // Minimum session lifetime
           'max_lifetime' => '2592000', // Maximum session lifetime (30 days)
       ],
   ],
Enter fullscreen mode Exit fullscreen mode

Redis for Cache Storage:

   'cache' => [
       'frontend' => [
           'default' => [
               'backend' => 'Cm_Cache_Backend_Redis',
               'backend_options' => [
                   'server' => '127.0.0.1',  // Redis server hostname
                   'port' => '6379',         // Redis server port
                   'persistent' => '',       // Persistent connection ID (optional)
                   'database' => '0',        // Redis database number for cache
                   'password' => '',         // Redis password (empty if none)
                   'compress_data' => '1',   // Enable data compression
               ],
           ],
           'page_cache' => [
               'backend' => 'Cm_Cache_Backend_Redis',
               'backend_options' => [
                   'server' => '127.0.0.1',  // Redis server hostname
                   'port' => '6379',         // Redis server port
                   'persistent' => '',       // Persistent connection ID (optional)
                   'database' => '1',        // Redis database number for full page cache
                   'password' => '',         // Redis password (empty if none)
                   'compress_data' => '1',   // Enable data compression
               ],
           ],
       ],
   ],
Enter fullscreen mode Exit fullscreen mode
  1. Save and exit the file.

Step 4: Clear Cache and Reindex Magento

  1. Clear the Magento cache:
   php bin/magento cache:clean
Enter fullscreen mode Exit fullscreen mode
  1. Reindex Magento:
   php bin/magento indexer:reindex
Enter fullscreen mode Exit fullscreen mode
  1. Finally, to check if Magento is now using Redis for sessions and caching, you can monitor Redis using the following command:
   redis-cli monitor
Enter fullscreen mode Exit fullscreen mode

If Magento is using Redis properly, you should see cache and session-related activity on the Redis server.

Step 5: Verify the Configuration

You can test the changes by logging in to your Magento Admin or Storefront and performing some actions, such as browsing the catalog or updating settings. If everything is working properly, Redis will be handling session storage and caching instead of the file system.


With Redis now handling cache and sessions, your Magento store should see improved performance, especially in high-traffic situations. Let me know if you run into any issues!


To change the ownership of all files to the user test-ssh and group clp, you can use the following command:

sudo chown -R test-ssh:clp /var/www/test.mgt.com
Enter fullscreen mode Exit fullscreen mode
  • The -R flag ensures the change is recursive, affecting all files and directories inside /var/www/test.mgt.com.
  • Replace /var/www/test.mgt.com with the path to the directory where your files are located, if different.

This will change the ownership of all files in the directory to the test-ssh user and the clp group.


To configure NGINX to run as user test-ssh and to create a PHP-FPM pool that runs under the user test-ssh and group clp, follow these steps:

Step 1: Configure NGINX to Run as User "test-ssh"

  1. Open the NGINX configuration file:
   sudo nano /etc/nginx/nginx.conf
Enter fullscreen mode Exit fullscreen mode
  1. Find the user directive (usually at the top of the file) and change it to test-ssh:
   user test-ssh clp;
Enter fullscreen mode Exit fullscreen mode
  1. Save the file and exit.

  2. Test the NGINX configuration:

   sudo nginx -t
Enter fullscreen mode Exit fullscreen mode
  1. Reload NGINX to apply the changes:
   sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a PHP-FPM Pool for User "test-ssh"

  1. Open the PHP-FPM pool configuration file. This is usually located in /etc/php/8.1/fpm/pool.d/ (adjust the PHP version as needed):
   sudo nano /etc/php/8.1/fpm/pool.d/test-ssh.conf
Enter fullscreen mode Exit fullscreen mode
  1. Add the following configuration to create a new pool:
   [test-ssh]
   user = test-ssh
   group = clp
   listen = /run/php/php8.1-fpm.test-ssh.sock
   listen.owner = test-ssh
   listen.group = clp
   listen.mode = 0660
   pm = dynamic
   pm.max_children = 5
   pm.start_servers = 2
   pm.min_spare_servers = 1
   pm.max_spare_servers = 3
   php_admin_value[error_log] = /var/log/php-fpm/test-ssh-error.log
   php_admin_flag[log_errors] = on
Enter fullscreen mode Exit fullscreen mode
  • Adjust the listen parameter and paths as necessary.
  1. Save the file and exit.

  2. Create the log directory if it doesn't exist:

   sudo mkdir -p /var/log/php-fpm
   sudo chown -R test-ssh:clp /var/log/php-fpm
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure NGINX to Use the PHP-FPM Pool

  1. Open your NGINX server block configuration file for your Magento site:
   sudo nano /etc/nginx/sites-available/test.mgt.com
Enter fullscreen mode Exit fullscreen mode
  1. Modify the location ~ \.php$ block to use the new PHP-FPM pool:
   location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/run/php/php8.1-fpm.test-ssh.sock;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }
Enter fullscreen mode Exit fullscreen mode
  1. Save the file and exit.

Step 4: Restart PHP-FPM and NGINX

  1. Restart the PHP-FPM service:
   sudo systemctl restart php8.1-fpm
Enter fullscreen mode Exit fullscreen mode
  1. Restart NGINX:
   sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Step 5: Verify the Configuration

To verify that everything is working correctly:

  1. Access your Magento site at http://test.mgt.com.
  2. Check the PHP-FPM logs in /var/log/php-fpm/test-ssh-error.log for any errors.
  3. Ensure that NGINX and PHP-FPM are running properly without errors:
   sudo systemctl status nginx
   sudo systemctl status php8.1-fpm
Enter fullscreen mode Exit fullscreen mode

With these steps, you have successfully configured NGINX to run as user test-ssh and created a PHP-FPM pool that also runs as test-ssh and group clp. If you have any further questions or run into issues, feel free to ask!


To configure phpMyAdmin on your server using the domain pma.mgt.com, follow these steps:

Step 1: Install phpMyAdmin

  1. First, update your package list:
   sudo apt update
Enter fullscreen mode Exit fullscreen mode
  1. Install phpMyAdmin:
   sudo apt install phpmyadmin -y
Enter fullscreen mode Exit fullscreen mode
  1. During the installation, you will be prompted to select the web server that should be automatically configured to run phpMyAdmin. Choose NGINX. If NGINX is not listed, you can skip this step and configure it manually later.

  2. When asked to use dbconfig-common to set up the database, select Yes and provide your MySQL credentials when prompted.

Step 2: Configure NGINX for phpMyAdmin

  1. Create a new server block configuration file for phpMyAdmin:
   sudo nano /etc/nginx/sites-available/pma.mgt.com
Enter fullscreen mode Exit fullscreen mode
  1. Add the following configuration:
   server {
       listen 80;
       server_name pma.mgt.com;

       root /usr/share/phpmyadmin;
       index index.php index.html index.htm;

       location / {
           try_files $uri $uri/ =404;
       }

       location ~ \.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Adjust if you have a different PHP version
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
       }

       location ~ /\.ht {
           deny all;
       }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Save the file and exit.

  2. Enable the new site configuration by creating a symbolic link to the sites-enabled directory:

   sudo ln -s /etc/nginx/sites-available/pma.mgt.com /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode
  1. Test the NGINX configuration:
   sudo nginx -t
Enter fullscreen mode Exit fullscreen mode
  1. Reload NGINX to apply the changes:
   sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure phpMyAdmin Access

  1. Open the phpMyAdmin configuration file:
   sudo nano /etc/phpmyadmin/config.inc.php
Enter fullscreen mode Exit fullscreen mode
  1. (Optional) You can add additional security settings here. For example, you can change the default username and password or restrict access by IP.

Step 4: Set Up Host Entry

If you haven't already done so, ensure your /etc/hosts file includes the following entry:

127.0.0.1    pma.mgt.com
Enter fullscreen mode Exit fullscreen mode

Step 5: Access phpMyAdmin

Now, you can access phpMyAdmin by navigating to http://pma.mgt.com in your web browser.

Step 6: Secure phpMyAdmin (Optional but Recommended)

  1. Basic Authentication: To secure phpMyAdmin with HTTP Basic Authentication, you can create a password for the user.
  • Create a password file:

     sudo htpasswd -c /etc/phpmyadmin/.htpasswd yourusername
    
  • Edit your NGINX server block for phpMyAdmin to include the following within the server block:

     location / {
         auth_basic "Restricted Access";
         auth_basic_user_file /etc/phpmyadmin/.htpasswd;
         try_files $uri $uri/ =404;
     }
    
  1. SSL Configuration: If you want to secure your connection to phpMyAdmin, consider setting up SSL. You can use tools like Certbot to obtain a free SSL certificate from Let's Encrypt.

After following these steps, you should have phpMyAdmin installed and accessible via pma.mgt.com. Let me know if you encounter any issues or have any questions!


To redirect HTTP to HTTPS and set up all store URLs to HTTPS in NGINX using a self-signed SSL certificate, follow these steps:

Step 1: Create a Self-Signed SSL Certificate

  1. Install the openssl package if it’s not already installed:
   sudo apt install openssl -y
Enter fullscreen mode Exit fullscreen mode
  1. Create a directory to store the SSL certificate and key:
   sudo mkdir /etc/nginx/ssl
Enter fullscreen mode Exit fullscreen mode
  1. Generate a self-signed SSL certificate:
   sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
   -keyout /etc/nginx/ssl/selfsigned.key \
   -out /etc/nginx/ssl/selfsigned.crt
Enter fullscreen mode Exit fullscreen mode

You will be prompted to enter information for the certificate. You can use the following details (for example):

  • Country Name: US
  • State or Province Name: California
  • Locality Name: Los Angeles
  • Organization Name: Your Company
  • Organizational Unit Name: IT
  • Common Name: pma.mgt.com
  • Email Address: your_email@example.com

Step 2: Configure NGINX for HTTPS

  1. Open the server block configuration file for your site:
   sudo nano /etc/nginx/sites-available/pma.mgt.com
Enter fullscreen mode Exit fullscreen mode
  1. Update the configuration to include the HTTPS settings and redirect HTTP traffic to HTTPS:
   server {
       listen 80;
       server_name pma.mgt.com;

       # Redirect all HTTP requests to HTTPS
       return 301 https://$host$request_uri;
   }

   server {
       listen 443 ssl;
       server_name pma.mgt.com;

       root /usr/share/phpmyadmin;
       index index.php index.html index.htm;

       ssl_certificate /etc/nginx/ssl/selfsigned.crt;
       ssl_certificate_key /etc/nginx/ssl/selfsigned.key;

       # Add SSL security settings (optional)
       ssl_protocols       TLSv1.2 TLSv1.3;
       ssl_ciphers         HIGH:!aNULL:!MD5;
       ssl_prefer_server_ciphers on;

       location / {
           try_files $uri $uri/ =404;
       }

       location ~ \.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Adjust if you have a different PHP version
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
       }

       location ~ /\.ht {
           deny all;
       }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Save the file and exit.

Step 3: Test and Reload NGINX

  1. Test the NGINX configuration:
   sudo nginx -t
Enter fullscreen mode Exit fullscreen mode
  1. If there are no errors, reload NGINX to apply the changes:
   sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Step 4: Update Store URLs in Magento

  1. Access your Magento root directory:
   cd /var/www/test.mgt.com
Enter fullscreen mode Exit fullscreen mode
  1. Update the base URLs in Magento to use HTTPS:
   php bin/magento setup:store-config:set --base-url=https://pma.mgt.com/ --base-url-secure=https://pma.mgt.com/
Enter fullscreen mode Exit fullscreen mode
  1. Clear the cache:
   php bin/magento cache:clean
   php bin/magento cache:flush
Enter fullscreen mode Exit fullscreen mode

Step 5: Verify Configuration

  • Open your web browser and navigate to http://pma.mgt.com. It should redirect to https://pma.mgt.com.
  • You might see a security warning due to the self-signed certificate. You can proceed past this warning to access phpMyAdmin over HTTPS.

With these steps, you've successfully redirected HTTP to HTTPS and configured your Magento store to use HTTPS with a self-signed certificate. If you have any questions or run into issues, feel free to ask!


To install and configure Varnish with Magento 2, follow these steps:

Step 1: Install Varnish

  1. Update your package list:
   sudo apt update
Enter fullscreen mode Exit fullscreen mode
  1. Install Varnish:
   sudo apt install varnish -y
Enter fullscreen mode Exit fullscreen mode
  1. Check the installed version to ensure it's installed correctly:
   varnishd -V
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure Varnish

  1. Open the Varnish configuration file:
   sudo nano /etc/varnish/default.vcl
Enter fullscreen mode Exit fullscreen mode
  1. Replace the contents with the following configuration. This configuration will set up Varnish to listen on port 80 and forward requests to the backend server (Magento) on port 8080:
   vcl 4.0;

   backend default {
       .host = "127.0.0.1";
       .port = "8080"; # Magento's default port
   }

   sub vcl_recv {
       # Remove the cookies from the request if not needed for caching
       if (req.http.Cookie) {
           unset req.http.Cookie;
       }

       return hash;
   }

   sub vcl_backend_response {
       # Set caching rules here
       if (beresp.status == 200) {
           set beresp.ttl = 1h; # Set the cache time for 1 hour
       }
   }

   sub vcl_deliver {
       # Remove the X-Varnish header
       unset resp.http.X-Varnish;
   }
Enter fullscreen mode Exit fullscreen mode
  1. Save the file and exit.

Step 3: Configure Magento to Use Varnish

  1. Open the Magento configuration file:
   sudo nano /etc/nginx/sites-available/test.mgt.com
Enter fullscreen mode Exit fullscreen mode
  1. Update the NGINX configuration to listen on port 8080 and to communicate with Varnish on port 80:
   server {
       listen 8080;
       server_name test.mgt.com;

       # Other existing configurations...

       location / {
           try_files $uri $uri/ /index.php?$query_string;
       }

       location ~ \.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Adjust if you have a different PHP version
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
       }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Save the file and exit.

Step 4: Update the Default Ports

  1. Edit the default systemd service file for Varnish to change the listening port from 80 to 6081 and specify the backend port as 8080. Open the service file:
   sudo nano /etc/systemd/system/varnish.service
Enter fullscreen mode Exit fullscreen mode
  1. Find the line that starts with ExecStart and change it to:
   ExecStart=/usr/sbin/varnishd -a :80 -t 120 -f /etc/varnish/default.vcl -p thread_pools=2 -p thread_pool_min=1 -p thread_pool_max=200 -p default_ttl=120 -p default_grace=30
Enter fullscreen mode Exit fullscreen mode

Step 5: Start and Enable Varnish

  1. Start the Varnish service:
   sudo systemctl start varnish
Enter fullscreen mode Exit fullscreen mode
  1. Enable Varnish to start on boot:
   sudo systemctl enable varnish
Enter fullscreen mode Exit fullscreen mode
  1. Check the status of Varnish to ensure it's running:
   sudo systemctl status varnish
Enter fullscreen mode Exit fullscreen mode

Step 6: Clear Magento Cache

Clear the cache in Magento to ensure the changes take effect:

php bin/magento cache:clean
php bin/magento cache:flush
Enter fullscreen mode Exit fullscreen mode

Step 7: Test the Configuration

  1. Navigate to your Magento store in a web browser using the domain name configured for Varnish (e.g., http://test.mgt.com).
  2. Check if Varnish is caching your pages by looking for the X-Varnish header in the response. You can use browser developer tools or a tool like curl:
   curl -I http://test.mgt.com
Enter fullscreen mode Exit fullscreen mode

Look for the X-Varnish header in the response.


With these steps, you've successfully installed Varnish and configured it to work with Magento 2. If you have any questions or run into issues, feel free to ask!

Top comments (0)