Step-by-Step Guide: How to Install Magento 2.4.5 with PHP 7.4 on Ubuntu 22.04 (LAMP Stack + Elasticsearch 7.x)
Share this:

Installing Magento 2.4.5 on Ubuntu 22.04 with PHP 7.4 requires careful preparation of your server environment, including Apache, MySQL, PHP, Composer, and Elasticsearch. This guide provides a detailed, step-by-step walkthrough to ensure a smooth installation, optimized for performance and security.

Prerequisites

Before you begin, ensure your system meets the following requirements:

  • Ubuntu 22.04 LTS (Jammy Jellyfish) installed and updated. Ubuntu 22.04 is the recommended OS for Magento 2.4.5 due to its stability and long-term support.
  • Root or sudo access to execute administrative commands.
  • Minimum 4GB RAM and 2 CPU cores for optimal performance. Magento 2.4.5 requires sufficient resources, especially during installation and indexing.
  • Stable internet connection to download packages and dependencies.
  • Domain name or server IP pointing to your server.

Magento 2.4.5 officially supports PHP 7.4 and 8.1, but this guide focuses on PHP 7.4 for compatibility with legacy extensions and themes. Elasticsearch 7.x is mandatory for catalog search functionality.

Step 1: Update and Upgrade System Packages

Start by updating your system’s package list and upgrading installed packages to their latest versions:

sudo apt update && sudo apt upgrade -y

This ensures all existing packages are up-to-date and reduces the risk of conflicts during installation.

Step 1.1: Install Required Dependencies

Install essential tools and libraries:

sudo apt install -y curl wget git software-properties-common apt-transport-https ca-certificates gnupg lsb-release

Step 2: Install Apache Web Server

Apache is the recommended web server for Magento 2. Install it with:

sudo apt install -y apache2

Enable Apache to start on boot and verify its status:

sudo systemctl enable apache2
sudo systemctl status apache2

If Apache is running, you should see an active (running) status.

Step 2.1: Configure Apache for Magento

Enable the rewrite module, which is required for Magento’s SEO-friendly URLs:

sudo a2enmod rewrite
sudo systemctl restart apache2

Edit the default Apache configuration to allow .htaccess overrides:

sudo nano /etc/apache2/sites-available/000-default.conf

Add the following within the <Directory /var/www/html> section:

    AllowOverride All
    Require all granted

Save the file and restart Apache:

sudo systemctl restart apache2

Step 3: Install PHP 7.4 and Required Extensions

Ubuntu 22.04 does not include PHP 7.4 in its default repositories. Add the ondrej/php PPA to install PHP 7.4:

sudo add-apt-repository ppa:ondrej/php
sudo apt update

Install PHP 7.4 and the required extensions for Magento:

sudo apt install -y php7.4 php7.4-cli php7.4-fpm php7.4-common php7.4-mysql php7.4-gd php7.4-curl php7.4-intl php7.4-xsl php7.4-mbstring php7.4-zip php7.4-bcmath php7.4-soap php7.4-xml php7.4-xmlrpc

Verify the installation:

php -v

You should see PHP 7.4.x in the output.

Step 3.1: Configure PHP for Magento

Edit the php.ini file to optimize PHP settings for Magento:

sudo nano /etc/php/7.4/apache2/php.ini

Update the following values:

memory_limit = 2G
max_execution_time = 1800
upload_max_filesize = 256M
max_input_time = 1800
date.timezone = UTC

Save the file and restart Apache:

sudo systemctl restart apache2

Step 4: Install and Configure MySQL 8.0

Magento 2.4.5 requires MySQL 8.0 or MariaDB 10.4. Install MySQL with:

sudo apt install -y mysql-server

Secure your MySQL installation:

sudo mysql_secure_installation

Follow the prompts to set a root password and remove insecure defaults.

Step 4.1: Create a Database for Magento

Log in to MySQL:

sudo mysql -u root -p

Create a database and user for Magento:

CREATE DATABASE magento2_db;
CREATE USER 'magento2_user'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON magento2_db.* TO 'magento2_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 5: Install and Configure Elasticsearch 7.x

Magento 2.4.5 requires Elasticsearch 7.x for catalog search. Install it with:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
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 -y elasticsearch

Enable and start Elasticsearch:

sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch

Verify Elasticsearch is running:

curl -X GET "localhost:9200/"

You should see a JSON response with Elasticsearch version details.

Step 6: Install Composer

Composer is required to manage Magento’s PHP dependencies. Install it globally:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

Verify the installation:

composer --version

Step 7: Download and Install Magento 2.4.5

Navigate to your web root directory:

cd /var/www/html

Use Composer to create a new Magento project:

sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.5 .

You will be prompted for your Magento Marketplace authentication keys. Enter them when prompted.

Step 7.1: Set File Permissions

Magento requires specific file permissions. Run the following commands:

sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
sudo chown -R :www-data .
sudo chmod u+x bin/magento

Step 7.2: Run the Magento Installation

Execute the Magento setup command:

sudo bin/magento setup:install \
--base-url=http://your_domain_or_ip \
--db-host=localhost \
--db-name=magento2_db \
--db-user=magento2_user \
--db-password=your_strong_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/Chicago \
--use-rewrites=1 \
--search-engine=elasticsearch7 \
--elasticsearch-host=localhost \
--elasticsearch-port=9200

Replace your_domain_or_ip and other placeholders with your actual values.

Step 7.3: Enable Developer Mode (Optional)

For development environments, enable developer mode:

sudo bin/magento deploy:mode:set developer

Step 8: Configure Cron Jobs

Magento relies on cron jobs for background tasks. Edit the crontab for the web server user:

sudo crontab -u www-data -e

Add the following lines:

* * * * * /usr/bin/php /var/www/html/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/html/var/log/magento.cron.log
* * * * * /usr/bin/php /var/www/html/update/cron.php >> /var/www/html/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/html/bin/magento setup:cron:run >> /var/www/html/var/log/setup.cron.log

Step 9: Access Magento Admin and Frontend

Navigate to your domain or server IP in a web browser:

  • Frontend: http://your_domain_or_ip
  • Admin Panel: http://your_domain_or_ip/admin

Log in with the admin credentials you specified during installation.

Pro Tips for Optimizing Magento 2.4.5 on Ubuntu 22.04

  • Enable OPcache: Improve PHP performance by enabling OPcache in php.ini:
    opcache.enable=1
    opcache.memory_consumption=512
  • Use Varnish Cache: Configure Varnish as a full-page cache to significantly improve page load times.
  • Optimize MySQL: Tune MySQL’s innodb_buffer_pool_size to at least 50% of available RAM for better database performance.
  • Disable Unused Modules: Use bin/magento module:disable to disable unnecessary modules and reduce overhead.
  • Regular Backups: Schedule automated backups of your Magento database and file system to prevent data loss.
  • Security Hardening: Change the default admin URL, enable two-factor authentication, and use a Web Application Firewall (WAF).
  • Monitor Performance: Use tools like New Relic or Blackfire to monitor and optimize Magento’s performance.
  • Keep Magento Updated: Regularly check for and apply Magento security patches and updates.

Frequently Asked Questions (FAQ)

Why does Magento 2.4.5 require PHP 7.4?

Magento 2.4.5 was tested and certified to run on PHP 7.4 and 8.1. PHP 7.4 offers a balance of performance, compatibility with older extensions, and security updates, making it a reliable choice for production environments.

Can I use PHP 8.1 instead of PHP 7.4?

Yes, Magento 2.4.5 fully supports PHP 8.1. If you prefer PHP 8.1, replace all instances of php7.4 with php8.1 in the commands above and ensure all required extensions are installed for PHP 8.1.

What if Elasticsearch is not installed?

Magento 2.4.5 will not function properly without Elasticsearch 7.x. The catalog search feature relies on Elasticsearch, and its absence will result in errors during installation and operation.

How do I troubleshoot a blank page after installation?

A blank page is often caused by incorrect file permissions, missing PHP extensions, or misconfigured Apache/NGINX. Check your server error logs (/var/log/apache2/error.log) and ensure all prerequisites are met.

Can I install Magento 2.4.5 on a server with less than 4GB RAM?

While possible, it is not recommended. Magento 2.4.5 requires significant resources, especially during installation and indexing. For development, you can use a swap file, but production environments should meet or exceed the recommended 4GB RAM.

How do I secure my Magento admin panel?

Change the default admin URL, use a strong password, enable two-factor authentication, restrict access by IP, and regularly update Magento and its extensions to protect against vulnerabilities.

What is the best way to back up my Magento store?

Use a combination of database dumps (mysqldump) and file system backups (tar or rsync). Automate backups and store them offsite for disaster recovery.

How do I upgrade from Magento 2.4.5 to a newer version?

Upgrading Magento involves updating Composer dependencies, running database migrations, and clearing caches. Always back up your store and test the upgrade in a staging environment before applying it to production.

Conclusion

Installing Magento 2.4.5 with PHP 7.4 on Ubuntu 22.04 is a multi-step process that requires careful attention to detail. By following this guide, you ensure a stable, secure, and high-performance eCommerce platform. Regular maintenance, security updates, and performance optimizations will keep your Magento store running smoothly and efficiently.

If you encounter issues or need further customization, consult the official Magento documentation or seek assistance from the Magento community.

Share this:

Leave a Reply