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
- Log into your AWS account.
- Navigate to the EC2 Dashboard.
- Click on Launch Instance.
- Choose an Amazon Machine Image (AMI):
- Search for Debian 11 in the community AMIs section.
- Select Debian 11 (Bullseye).
- Choose an instance type (e.g., t2.medium for better performance).
- Configure your instance settings (like VPC, subnets, and security groups).
- Add storage (at least 20 GB).
- Review and launch the instance.
After launching, access the instance via SSH:
ssh -i your-key.pem admin@your-instance-public-ip
Step 2: Update the System
Before installing any software, ensure your system is up to date:
sudo apt update
sudo apt upgrade -y
Step 3: Install NGINX
- Install NGINX:
sudo apt install nginx -y
- Start and enable NGINX:
sudo systemctl start nginx
sudo systemctl enable nginx
Step 4: Install PHP 8.1
- Install required dependencies:
sudo apt install software-properties-common -y
- Add the PHP repository for version 8.1:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
- 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
- Start and enable PHP-FPM:
sudo systemctl start php8.1-fpm
sudo systemctl enable php8.1-fpm
Step 5: Install MySQL 8
- 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
- When prompted, select MySQL 8.0 and press Enter.
- Update the package list and install MySQL 8:
sudo apt update
sudo apt install mysql-server -y
- Start and enable MySQL:
sudo systemctl start mysql
sudo systemctl enable mysql
- Secure the MySQL installation:
sudo mysql_secure_installation
- Set a strong password for the root user, remove anonymous users, disable remote root login, and remove the test database.
Step 6: Install Elasticsearch
- 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'
- Update the package list and install Elasticsearch:
sudo apt update
sudo apt install elasticsearch -y
- Configure Elasticsearch: Open the Elasticsearch configuration file:
sudo nano /etc/elasticsearch/elasticsearch.yml
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)
- Start and enable Elasticsearch:
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch
- Test that Elasticsearch is running:
curl -X GET "localhost:9200/"
You should see a JSON response with details about the Elasticsearch node.
Step 7: Configure NGINX for PHP 8.1
- Create an NGINX server block for PHP:
sudo nano /etc/nginx/sites-available/default
- 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;
}
}
- Test the NGINX configuration:
sudo nginx -t
- Reload NGINX to apply changes:
sudo systemctl reload nginx
Step 8: Test PHP and MySQL
- Create a test PHP file:
sudo nano /var/www/html/info.php
Add the following content:
<?php
phpinfo();
?>
- 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.
- Open the
/etc/hosts
file:
sudo nano /etc/hosts
- Add the following entry to associate
test.mgt.com
withlocalhost
:
127.0.0.1 test.mgt.com
- Save and exit.
Step 2: Set Up Web Root and Permissions
- Create a directory for Magento inside the web root:
sudo mkdir -p /var/www/test.mgt.com
- 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
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
Step 4: Install Magento 2 via Composer
- Navigate to the Magento directory:
cd /var/www/test.mgt.com
- Install Magento 2 using Composer:
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.6 .
- 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)
- To install Magento with Sample Data, run the following command:
php bin/magento sampledata:deploy
- Update the Magento setup after installing the sample data:
php bin/magento setup:upgrade
Step 6: Configure NGINX for Magento
- Create a new NGINX server block for Magento:
sudo nano /etc/nginx/sites-available/test.mgt.com
- 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;
}
}
- Enable the configuration:
sudo ln -s /etc/nginx/sites-available/test.mgt.com /etc/nginx/sites-enabled/
- Test the configuration:
sudo nginx -t
- Reload NGINX:
sudo systemctl reload nginx
Step 7: Configure Elasticsearch for Magento
Magento 2.4.x requires Elasticsearch as the search engine. Follow these steps to configure it:
- Verify that Elasticsearch is running:
curl -X GET "localhost:9200/"
- Ensure that Magento connects to Elasticsearch using the correct host and port (
localhost:9200
by default).
Step 8: Magento Installation via CLI
- 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"
- 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
Step 9: Finalize Setup
- Disable Magento Maintenance Mode (if it's enabled):
php bin/magento maintenance:disable
- Reindex the Magento store:
php bin/magento indexer:reindex
- Deploy Static Content:
php bin/magento setup:static-content:deploy -f
- Clear Cache:
php bin/magento cache:clean
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
- Update your package list and install Redis:
sudo apt update
sudo apt install redis-server -y
- Once installed, check if Redis is running:
sudo systemctl status redis
You should see Redis in an active/running state.
- Enable Redis to start on boot:
sudo systemctl enable redis-server
- Verify Redis is working by connecting to the Redis CLI:
redis-cli
Then run the ping
command, and you should get a response like this:
PONG
Step 2: Configure Redis for Magento 2
Before configuring Magento, modify the Redis configuration for performance and security:
- Open the Redis configuration file:
sudo nano /etc/redis/redis.conf
-
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
Restart Redis to apply the changes:
sudo systemctl restart redis-server
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.
- Open the Magento configuration file (
env.php
):
sudo nano /var/www/test.mgt.com/app/etc/env.php
- 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)
],
],
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
],
],
],
],
- Save and exit the file.
Step 4: Clear Cache and Reindex Magento
- Clear the Magento cache:
php bin/magento cache:clean
- Reindex Magento:
php bin/magento indexer:reindex
- Finally, to check if Magento is now using Redis for sessions and caching, you can monitor Redis using the following command:
redis-cli monitor
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
- 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"
- Open the NGINX configuration file:
sudo nano /etc/nginx/nginx.conf
- Find the
user
directive (usually at the top of the file) and change it totest-ssh
:
user test-ssh clp;
Save the file and exit.
Test the NGINX configuration:
sudo nginx -t
- Reload NGINX to apply the changes:
sudo systemctl reload nginx
Step 2: Create a PHP-FPM Pool for User "test-ssh"
- 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
- 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
- Adjust the
listen
parameter and paths as necessary.
Save the file and exit.
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
Step 3: Configure NGINX to Use the PHP-FPM Pool
- Open your NGINX server block configuration file for your Magento site:
sudo nano /etc/nginx/sites-available/test.mgt.com
- 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;
}
- Save the file and exit.
Step 4: Restart PHP-FPM and NGINX
- Restart the PHP-FPM service:
sudo systemctl restart php8.1-fpm
- Restart NGINX:
sudo systemctl restart nginx
Step 5: Verify the Configuration
To verify that everything is working correctly:
- Access your Magento site at
http://test.mgt.com
. - Check the PHP-FPM logs in
/var/log/php-fpm/test-ssh-error.log
for any errors. - Ensure that NGINX and PHP-FPM are running properly without errors:
sudo systemctl status nginx
sudo systemctl status php8.1-fpm
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
- First, update your package list:
sudo apt update
- Install phpMyAdmin:
sudo apt install phpmyadmin -y
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.
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
- Create a new server block configuration file for phpMyAdmin:
sudo nano /etc/nginx/sites-available/pma.mgt.com
- 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;
}
}
Save the file and exit.
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/
- Test the NGINX configuration:
sudo nginx -t
- Reload NGINX to apply the changes:
sudo systemctl reload nginx
Step 3: Configure phpMyAdmin Access
- Open the phpMyAdmin configuration file:
sudo nano /etc/phpmyadmin/config.inc.php
- (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
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)
- 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; }
- 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
- Install the
openssl
package if it’s not already installed:
sudo apt install openssl -y
- Create a directory to store the SSL certificate and key:
sudo mkdir /etc/nginx/ssl
- 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
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
- Open the server block configuration file for your site:
sudo nano /etc/nginx/sites-available/pma.mgt.com
- 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;
}
}
- Save the file and exit.
Step 3: Test and Reload NGINX
- Test the NGINX configuration:
sudo nginx -t
- If there are no errors, reload NGINX to apply the changes:
sudo systemctl reload nginx
Step 4: Update Store URLs in Magento
- Access your Magento root directory:
cd /var/www/test.mgt.com
- 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/
- Clear the cache:
php bin/magento cache:clean
php bin/magento cache:flush
Step 5: Verify Configuration
- Open your web browser and navigate to
http://pma.mgt.com
. It should redirect tohttps://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
- Update your package list:
sudo apt update
- Install Varnish:
sudo apt install varnish -y
- Check the installed version to ensure it's installed correctly:
varnishd -V
Step 2: Configure Varnish
- Open the Varnish configuration file:
sudo nano /etc/varnish/default.vcl
- 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;
}
- Save the file and exit.
Step 3: Configure Magento to Use Varnish
- Open the Magento configuration file:
sudo nano /etc/nginx/sites-available/test.mgt.com
- 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;
}
}
- Save the file and exit.
Step 4: Update the Default Ports
- 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
- 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
Step 5: Start and Enable Varnish
- Start the Varnish service:
sudo systemctl start varnish
- Enable Varnish to start on boot:
sudo systemctl enable varnish
- Check the status of Varnish to ensure it's running:
sudo systemctl status varnish
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
Step 7: Test the Configuration
- Navigate to your Magento store in a web browser using the domain name configured for Varnish (e.g.,
http://test.mgt.com
). - 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
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)