This guide walks you through installing Magento 2.4.5 on an Ubuntu 22.04 server. Follow the steps below to set up your Magento store.

Prerequisites

  • A fresh Ubuntu 22.04 server
  • Root or sudo access to the server
  • A domain name pointing to your server

Step 1: Update Your System

First, ensure your system is up to date.

bash
sudo apt update && sudo apt upgrade -y

Step 2: Install Apache, MySQL, and PHP

Magento requires a web server, a database server, and PHP. Install Apache, MySQL, and PHP with the required extensions.

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade
bash
sudo apt install apache2 mysql-server php php-cli php-fpm php-mysql php-zip php-intl php-mbstring php-xml php-curl php-soap php-gd php-bcmath libapache2-mod-php -y

Step 3: Configure MySQL

Secure your MySQL installation and create a database for Magento.

bash
sudo mysql_secure_installation

Log in to the MySQL shell and create a database and user.

sql
sudo mysql -u root -p
CREATE DATABASE magento;
CREATE USER 'magentouser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON magento.* TO 'magentouser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 4: Install and Configure Elasticsearch

Magento uses Elasticsearch for its search functionality. Install Elasticsearch.

bash
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /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

Magento requires Composer for dependency management. Install Composer.

bash
sudo apt install curl
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Step 6: Download Magento

Download the Magento 2.4.5 package from the official Magento repository.

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

Step 7: Set the Correct Permissions

Set the correct permissions for the Magento directory.

bash
sudo chown -R www-data:www-data /var/www/html/magento
sudo chmod -R 755 /var/www/html/magento
sudo chmod -R 775 /var/www/html/magento/{var,app/etc,pub/static,pub/media}

Step 8: Configure Apache

Create a new virtual host configuration file for Magento.

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

Add the following content:

apache
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html/magento
ServerName example.com
ServerAlias www.example.com
<Directory /var/www/html/magento>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>ErrorLog ${APACHE_LOG_DIR}/magento_error.log
CustomLog ${APACHE_LOG_DIR}/magento_access.log combined
</VirtualHost>

Enable the new virtual host and rewrite module, then restart Apache.

bash
sudo a2ensite magento.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

Step 9: Install Magento

Access your Magento installation through your web browser by navigating to http://example.com and follow the on-screen instructions to complete the installation.

Step 10: Post-Installation Tasks

After installation, configure Magento to use Elasticsearch and set the correct permissions again.

bash
cd /var/www/html/magento
bin/magento config:set catalog/search/engine elasticsearch7
bin/magento config:set catalog/search/elasticsearch7_server_hostname localhost
bin/magento config:set catalog/search/elasticsearch7_server_port 9200
Configure Two-Factor Authorization in order to proceed 
bin/magento module:disable Magento_TwoFactorAuth

Show errors in php on ubuntu?

Edit that file and find display_errors. Change the value to On

Switch php versions on commandline ubuntu 16.04

sudo update-alternatives --config php 
sudo service apache2 restart 

.htaccess: Invalid command ‘RewriteEngine’, perhaps misspelled or defined by a module not included in the server configuration

sudo a2enmod rewrite && sudo service apache2 restart

Disable the Magento TwoFactorAuth module

bin/magento module:disable Magento_TwoFactorAuth