Install Magento 2.4.5 or Magento 2.4.6 on Ubuntu 22.04 with Apache2 and PHP 8.1

Follow the guide to install Magento 2.4.5 or Magento 2.4.6 on Ubuntu 22.04 with PHP 8.1, including configuring password access for the MySQL root account and installing/configuring Elasticsearch.

Step 1: Update and Upgrade System Packages

bash
sudo apt update sudo apt upgrade

Step 2: Install Apache2

bash
sudo apt install apache2
sudo systemctl enable apache2
sudo systemctl start apache2

Step 3: Enable Apache2 Modules

Enable required Apache modules:

bash
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod headers
Restart Apache to apply the changes:
bash
sudo systemctl restart apache2
Step 4: Configure Apache Virtual Host for Magento
Create a new Apache configuration file for your Magento site:
bash
sudo nano /etc/apache2/sites-available/magento.conf
Add the following configuration:
apache
<VirtualHost *:80>
ServerAdmin webmaster@your_domain
DocumentRoot /var/www/html/magento
ServerName your_domain
<Directory /var/www/html/magento/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted</Directory>ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 5: Enable the Magento Site

bash

sudo a2ensite magento.conf

Step 6: Configure Hosts File

Edit the hosts file to map your domain to the server’s IP address:
bash

sudo nano /etc/host

Add the following line:

plaintext

127.0.0.1 your_domain

Step 7: Restart Apache

Restart Apache to apply the changes:

bash
sudo systemctl restart apache2

Step 8: Set File Permissions

bash
sudo chown -R www-data:www-data /var/www/html/magento
sudo chmod -R 755 /var/www/html/magento

Step 9: Test Your Configuration

Open your web browser and navigate to http://your_domain. You should see the default Apache page or a Magento installation page if you have already placed Magento files in the specified directory.

sudo systemctl enable apache2
sudo systemctl start apache2

Step 2: Install MySQL and Create Database for Magento 2

bash
sudo apt install mysql-server
sudo systemctl enable mysql
sudo systemctl start mysql
sudo mysql_secure_installation

Create a database and user:

sql
CREATE DATABASE magento_db;
CREATE USER 'magento_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL ON magento_db.* TO 'magento_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 3: Install PHP and Required Extensions

bash
sudo apt install php8.1 libapache2-mod-php8.1 php8.1-cli php8.1-fpm php8.1-json php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-pear php8.1-bcmath php8.1-json

Edit the php.ini file:

bash
sudo nano /etc/php/8.1/apache2/php.ini

Ensure the following settings:

ini
file_uploads = On
allow_url_fopen = On
memory_limit = 512M
upload_max_filesize = 256M
max_execution_time = 360
date.timezone = [Your_Timezone]

Restart Apache:

bash
sudo systemctl restart apache2

Step 4: Install and Configure Elasticsearch

bash
sudo apt install openjdk-11-jre
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'
sudo apt update
sudo apt install elasticsearch
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch

Step 5: Install Composer

bash
sudo apt install composer

Step 6: Download and Install Magento 2

bash
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento2
cd magento2

Step 7: Configure MySQL Connection in Magento

bash
sudo bin/magento setup:install --base-url=http://your_domain/ \
--db-host=localhost --db-name=magento_db --db-user=magento_user --db-password=your_password \
--admin-firstname=Admin --admin-lastname=User --admin-email=admin@example.com \
--admin-user=admin --admin-password=admin123 --language=en_US \
--currency=USD --timezone=America/New_York --use-rewrites=1

Step 8: Configure Apache for Magento

Create a new Apache configuration file:

bash
sudo nano /etc/apache2/sites-available/magento2.conf

Add the following configuration:

apache
<VirtualHost *:80>
ServerAdmin webmaster@your_domain
DocumentRoot /var/www/html/magento2
ServerName your_domain
<Directory /var/www/html/magento2/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the site and restart Apache:

bash
sudo a2ensite magento2.conf
sudo systemctl restart apache2

Step 9: Set File Permissions

bash
sudo chown -R www-data:www-data /var/www/html/magento2
sudo chmod -R 755 /var/www/html/magento2