How to Install Magento 2.4.6 on Ubuntu 22.04 for a Supercharged Ecommerce Site

Welcome, fellow Magento enthusiasts! Are you ready to embark on an exciting journey to install Magento 2.4.6 on Ubuntu 22.04? Buckle up, because this step-by-step guide will make the process smoother than a well-churned gelato. Let’s dive in and sprinkle a little humor along the way to keep things fun and engaging.

Magento 2.4.6 System Requirements

Before we get our hands dirty, let’s check if our system meets the essential requirements. Here’s what you need for a flawless Magento 2.4.6 installation:

  • Operating System: Linux x86-64 (like Ubuntu, Debian, or their friends).
  • Web Server: Apache 2.4 or later, or NGINX 1.18 or later.
  • Database: MySQL 8.0 or later, or MariaDB 10.4 or later.
  • PHP Version: PHP 8.1 or later. Extensions needed: bcmath, ctype, curl, dom, gd, hash, iconv, intl, mbstring, openssl, pdo_mysql, simplexml, soap, xsl, zip, sockets, xml, xmlreader, xmlwriter, and libxml.
  • SSL: A valid security certificate for secure transactions (because HTTPS is the cool and safe kid on the block).

Prerequisites for Magento 2.4.6

  1. System Requirements: Check! We've got those covered.
  2. Managed Hosting: Investing in a managed hosting plan can save you from sleepless nights of troubleshooting.

Steps for Magento 2.4.6 Installation on Ubuntu 22.04

Step 1: Update Your Operating System

Start by updating your OS to ensure all packages are up-to-date:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx Web Server

Install Nginx, your web server pal, with these commands:

sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx

Step 3: Install PHP and PHP Extensions

Get PHP 8.1 and the necessary extensions (like getting toppings for your pizza):

sudo apt-get install php php-dev php-fpm php-bcmath php-intl php-soap php-zip
php-curl php-mbstring php-mysql php-gd php-xml
php -v

Step 4: Update php.ini File

Customise your PHP settings to make Magento happy:

php --ini | grep "Loaded Configuration File"
sudo nano /etc/php/8.1/cli/php.ini

Set the following parameters:

  • file_uploads = On
  • allow_url_fopen = On
  • short_open_tag = On
  • memory_limit = 512M
  • upload_max_filesize = 128M
  • max_execution_time = 3600

Restart Nginx to apply changes:

sudo systemctl restart nginx

Step 5: Install MySQL 8 and Create a Database

Install MySQL and set up your Magento database:

sudo apt install mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql
mysql -u root -p

Inside the MySQL prompt, create a database and user:

CREATE DATABASE magentodb;
CREATE USER 'magentouser'@'localhost' IDENTIFIED BY 'MyPassword';
GRANT ALL ON magentodb.* TO 'magentouser'@'localhost';
FLUSH PRIVILEGES;
EXIT

Step 6: Install Elasticsearch

Configure Elasticsearch for Magento’s advanced search capabilities:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg]
https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
sudo apt update && sudo apt install elasticsearch
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch

Open the Elasticsearch configuration file and make necessary adjustments:

sudo nano /etc/elasticsearch/elasticsearch.yml

Disable security features for simplicity:

xpack.security.enabled: false

Restart Elasticsearch to apply changes:

sudo systemctl restart elasticsearch

Verify Elasticsearch is running correctly:

curl -X GET "localhost:9200/"

You should see a friendly JSON output saying, "You Know, for Search."

Step 7: Install Composer

Install Composer, the PHP dependency manager:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
composer --version

Step 8: Install Magento 2.4.6

Time to bring in the main act—installing Magento!

First, generate your Magento Access Keys from the Magento Marketplace.

Download Magento using Composer:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.6 /var/www/magento2

When prompted, enter your Public Key as the username and Private Key as the password.

Navigate to the Magento directory:

cd /var/www/magento2

Set appropriate permissions for cache and static content folders:

find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
sudo chown -R www-data:www-data /var/www/magento2
sudo chmod -R 755 /var/www/magento2

Install Magento using the Composer command (customize parameters to your environment):

bin/magento setup:install \
--base-url=http://your-domain.com \
--db-host=localhost \
--db-name=magentodb \
--db-user=magentouser \
--db-password=MyPassword \
--admin-firstname=Admin \
--admin-lastname=User \
[email protected] \
--admin-user=admin \
--admin-password=admin123 \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1
--elasticsearch-host=your-elasticsearch-host \
--elasticsearch-port=9200 # Adjust the port if different

You'll receive a success message and an admin link for your Magento site.

Step 9: Configure Nginx Web Server for Magento 2.4.6

Create a configuration file for your Magento installation:

sudo nano /etc/nginx/conf.d/magento2.conf

Add the following content, customizing it as needed (replace your-domain.com with your actual domain):

nginx

upstream fastcgi_backend {
  server  unix:/run/php/php8.1-fpm.sock;
}

server {

  listen 80;
  server_name your-domain.com www.your-domain.com;
  set $MAGE_ROOT /var/www/magento2;
  include /var/www/magento2/nginx.conf.sample;
}

Before applying the configuration ensure the php-fpm sock path by

cat /etc/php/8.1/fpm/pool.d/www.conf
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php8.1-fpm.sock

Which may differ in some scenarios.

After saving the nginx conf check for any errors by

Sudo nginx -t

Restart Nginx to apply the configuration changes:

sudo systemctl restart nginx

Step 10: Access Your Magento 2.4.6 Application

Now that your Magento installation is ready, open your preferred web browser and type your domain (e.g., http://your-domain.com) in the address bar. Voilà, you’ve got your Magento site up and running!

Summary

Installing Magento 2.4.6 on Ubuntu is a straightforward process that can be done step-by-step. It allows you to harness the new features and enhancements in Magento 2.4.6, all within the secure framework of the Ubuntu operating system. This tutorial covered the steps to install Magento 2.4.6 on Ubuntu 22.04. It also highlighted system prerequisites, Nginx web server setup, PHP configuration, and Elasticsearch integration.

Choose a reliable Magento server hosting plan to ensure a solid base for your Magento 2.4.6 installation.