Friday, June 14, 2024
HomeHow toHow to Install Magento eCommerce Platform on Ubuntu 24.04

How to Install Magento eCommerce Platform on Ubuntu 24.04

Magento is an open-source and well-known eCommerce platform written in PHP. Magento is a robust and powerful eCommerce solution used by more than 240.000 merchants worldwide. Magento is a versatile and scalable platform for building an eCommerce store. It’s suitable for building small, medium, and big online stores. Magento allows you to create and host your online stores on your server.

In this guide, we’ll walk you through the installation of Magento eCommerce on the Ubuntu 22.04 server.

Prerequisites

To start with this guide, make sure you have prepared the following:

  • An Ubuntu 24.04 with at least 4GB of memory.
  • A non-root user with sudo privileges.
  • A domain name pointed to a server IP address.

Installing Dependencies

Before you jump to the installation, you must ensure that Magento dependencies are installed. In this section, you will install software packages for Magento, which include OpenSearch, Nginx, MySQL server, PHP-FPM, Redis, Composer, and Git.

First, install the ‘apt-transport-https‘ and ‘gnupg‘ packages with the following:

sudo apt install apt-transport-https gnupg -y

Run the following command to add the OpenSearch GPG key and repository for Debian/Ubuntu-based distributions.

curl -o- https://artifacts.opensearch.org/publickeys/opensearch.pgp | \
sudo gpg --dearmor --batch --yes -o /usr/share/keyrings/opensearch-keyring
echo "deb [signed-by=/usr/share/keyrings/opensearch-keyring] https://artifacts.opensearch.org/releases/bundle/opensearch/2.x/apt stable main" | \
sudo tee /etc/apt/sources.list.d/opensearch-2.x.list

Now update your Ubuntu package index and install dependencies with the following apt command. With this, you will install the OpenSearch, Nginx, MySQL, PHP-FPM, Redis, Composer, and Git packages.

sudo apt update && sudo apt install opensearch nginx mysql-server composer git redis-server php-fpm php-bcmath php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-redis php-ldap php-zip php-curl php-imagick

Input Y to confirm the installation.

After the installation, check all the services to ensure it is running.

Check the Nginx service with the following. You will see the Nginx service is running and enabled.

sudo systemctl is-enabled nginx
sudo systemctl status nginx

Now check the MySQL server with the command below. You can see in the output below that the MySQL server is running.

sudo systemctl is-enabled mysql
sudo systemctl status mysql

Check the PHP-FPM service using the following command. The PHP-FPM should be running and enabled automatically.

sudo systemctl is-enabled php8.3-fpm
sudo systemctl status php8.3-fpm

Check the Redis server using the command below. You should see that Redis is running and enabled.

sudo systemctl is-enabled redis-server
sudo systemctl status redis-server

Lastly, check the Composer version with the following. You should have Composer v2.7 installed on your system.

sudo -u www-data composer -v

Setting up OpenSearch

After you have installed dependencies, you need to configure OpenSearch. Magento used OpenSearch for advanced search pri=ducts and indexing. In this section, you will increase the default max heap memory and disable OpenSearch’s security plugin (only for a development environment).

Open the file ‘/etc/opensearch/jvm.options‘ using the following nano editor.

sudo nano /etc/opensearch/jvm.options

Change the default memory allocation for OpenSearch as you need. In this case, we’ll be using 2GB for OpenSearch.

-Xms2g
-Xmx2g

Save the file and exit.

Now open the default OpenSearch configuration ‘/etc/opensearch/opensearch.yml‘ with the nano editor.

sudo nano /etc/opensearch/opensearch.yml

Add the following configuration to disable the security plugin in OpenSearch. For demonstration, you can disable this, but for production, must enable the security plugin and set up TLS certificates for your OpenSearch installation.

plugins.security.disabled: true

When done, save and exit the file.

Next, run the command below to reload the systemd manager. After you have installed OpenSearch, you must reload the systemd manager.

sudo systemctl daemon-reload

Now start, enable, and verify the Opensearch service with the following command:

sudo systemctl enable --now opensearch
sudo systemctl status opensearch

In the following output, you can see that OpenSearch is running and enabled.

Lastly, check your OpenSearch installation using the following ‘curl‘ command. This will show you basic information about your OpenSearch installation.

curl -X GET http://localhost:9200

Below you can see OpenSearch 2.14 is running on the Ubuntu server.

Setting up PHP-FPM

In this section, you will modify the default PHP-FPM configuration and enable the Opcache extension through the ‘php.ini’ file. You will increase the default memory_limit and adjust the date.timezone option.

Open the PHP-FPM configuration ‘/etc/php/8.3/fpm/php.ini’ with the following nano editor.

sudo nano /etc/php/8.3/fpm/php.ini

Change the default configuration with the following. Also, adjust the date.timezone, and memory_limit with your current server environment.

date.timezone = Europe/Amsterdam

When done, save the file and exit.

Now run the command below to restart the PHP-FPM service and apply your changes.

sudo systemctl restart php8.3-fpm

Setting up MySQL server

Now that you have configured PHP-FPM, in the next section you will set up the MySQL server installation and create a new database and user for Magento.

Secure your MySQL server installation by executing the command below.

sudo mysql_secure_installation

Below you you will be asked about the MySQL server configurations:

  • Input y to enable the VALIDATE PASSWORD component.
  • Input 1 to set up password level strength to MEDIUM.
  • Input y when asked to change your MySQL root password.
  • Input y to remove the default anonymous user.
  • Input y to disallow remote login for the MySQL root user.
  • Input y again to remove the default database test from MySQL.
  • Lastly, input y to reload MySQL tables privileges apply changes.

After you have configured MySQL, you need to create a new database and user for Magento.

Log in to the MySQL server with the following command. Type your MySQL root password when prompted.

sudo mysql -u root -p

Now run the following queries to create a new database and user for Magento. In this example, you will create a new user and database ‘magento‘ with the password ‘M4gentoPassw0rd__‘.

Make sure to change the following details with your information. Also, the MySQL password for Magento should have a special character, number, and Uppercase.

CREATE DATABASE magento;
CREATE USER 'magento'@'localhost' IDENTIFIED BY 'M4gentoPassw0rd__';
GRANT ALL PRIVILEGES ON magento.* to 'magento'@'localhost';
FLUSH PRIVILEGES;

Next, run the following query to verify privileges for user ‘magento‘. Make sure your user can access the database ‘magento‘.

SHOW GRANTS FOR magento@localhost;

Lastly, type quit to exit from the MySQL server.

Installing Magento through the command line

In this section, you will download Magento source code and install PHP dependencies using Composer. Then, you will install Magento using the command line. In the last, you will generate a crontab for Magento.

Go to the ‘/var/www’ directory and download the Magento source code using the following wget command. Check the Magento GitHub page and grab the Magento latest version URL.

cd /var/www
wget https://github.com/magento/magento2/archive/refs/tags/2.4.7.tar.gz

After the download is complete, extract the Magento source code and rename the extracted directory to ‘magento2’. With this, your Magento installation should be located at ‘/var/www/magento2’.

tar -xf 2.4.7.tar.gz
mv magento2-* magento2

Now run the command below to create additional directories for Composer and change the ownership of the ‘/var/www’ directory to user ‘www-data‘.

sudo mkdir -p /var/www/{.config,.cache}
sudo chown -R www-data:www-data /var/www

Then, run the following command to give the ‘www-data‘ user permission to read, write, and execute within the ‘/var/www/magento2‘ directory.

sudo chmod u+rwx /var/www/magento2

Next, go to the ‘/var/www/magento2‘ directory and install PHP dependencies for Magento with the Composer command below. This will ensure your dependencies for Magento is installed

cd /var/www/magento2
sudo -u www-data composer install

After installing dependencies, run the command below to allow ‘www-data‘ user permission of ‘rwx‘ to both ‘generated‘ and ‘var‘ directories within the Magento web root.

sudo chown -R www-data:www-data /var/www/magento2/var /var/www/magento2/generated
sudo chmod u+rwx /var/www/magento2/var /var/www/magento2/generated
sudo chmod g+rw /var/www/magento2/var /var/www/magento2/generated

Now run the following command to start the Magento installation. Make sure to change the URL details, MySQL database, administrator user, and password for your Magento installation.

sudo -u www-data bin/magento setup:install \
--base-url=http://hwdomain.io \
--db-host=localhost \
--db-name=magento2 \
--db-user=magento \
--db-password=M4gentoPassw0rd__ \
--admin-firstname=bob \
--admin-lastname=admin \
[email protected] \
--admin-user=bob \
--admin-password=BobAdm1nP4ssword \
--currency=USD \
--timezone=Europe/Amsterdam \
--use-rewrites=1 \
--session-save=redis \
--search-engine=opensearch \
--opensearch-host=127.0.0.1 \
--opensearch-port=9200 \
--opensearch-enable-auth=0 \
--opensearch-index-prefix=magento22 \
--opensearch-timeout=30

Below you can see the Magento installation begin:

Once complete, you will see the output ‘Magento installation complete.’ and the admin URL path for your Magento installation. Make sure to save the generated URL for the Magento admin page.

To finish the Magento installation, run the following command to generate crontab for Magento and run indexing for Magento.

sudo -u www-data bin/magento cron:install
sudo -u www-data bin/magento cron:run --group index

You can check the list crontab for user ‘www-data’ with the following:

crontab -u www-data -l

You can see below the crontab for Magento is generated.

Lastly, run the command below to clear the cache of your Magento installation. With this, you’ve completed the Magento installation, next you need to set up the Nginx web server.

sudo -u www-data bin/magento cache:clean

Setting up Nginx server block

At this point, your Magento installation is finished, now you will create a new Nginx server to run your Magento installation. Ensure you have a domain name pointed to your server IP address.

Create a new Nginx server block configuration ‘/etc/nginx/sites-available/magento.conf‘ with the nano editor.

sudo nano /etc/nginx/sites-available/magento.conf

Insert the following configuration and make sure to change the server_name option with your Magento domain name.

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

server {
listen 80;
listen [::]:80;
server_name hwdomain.io;
set $MAGE_ROOT /var/www/magento2/;
include /var/www/magento2/nginx.conf.sample;
client_max_body_size 2M;

access_log /var/log/nginx/magento.access;
error_log /var/log/nginx/magento.error;
}

When finished, save and exit the file.

Now run the command below to activate the server block ‘magento.conf‘ and verify your Nginx syntax. Ensure you get the output ‘syntax is ok – test is successful‘.

sudo ln -s /etc/nginx/sites-available/magento.conf /etc/nginx/sites-enabled/
sudo nginx -t

Lastly, restart the Nginx service with the following to apply your new server block configuration. After the command is executed, your Magento installation will be available.

sudo systemctl restart nginx

Securing Magento with HTTPS

If you’re running Magento in the public domain, you must secure it with HTTPS. In this section, you will secure your Magento installation with HTTPS through Certbot and Letsencrypt.

Install the Certbot and Certbot Nginx plugin using the following command. Enter Y to confirm the installation.

sudo apt install certbot python3-certbot-nginx

After the installation is complete, run the command below to generate SSL/TLS certificates from Letsencrypt. Make sure to change the email address and domain name below with your information.

sudo certbot --nginx --agree-tos --no-eff-email --redirect --hsts --staple-ocsp --email [email protected] -d hwdomain.io

Your Magento installation should be automatically secured with HTTPS when the process is finished. Your SSL/TLS certificates are in the ‘/etc/letsencrypt/live/domain.com‘ directory.

Accessing Magento

Open your web browser and visit your Magento domain name such as https://hwdomain.io. if your installation is successful, you will see the default Magento index page like the following.

Now, visit the Magento administration URL, which is https://hwdomain.io/admin_ze87mmj. See the installation process.

Enter your admin user and password for Magento and press Login.

If successful, you should get the Magento administration dashboard like the following:

Conclusion

Congratulations! You have completed the installation of Magento eCommerce on the Ubuntu 24.04 server. You have Magento installed with the LEMP Stack (Linux, Nginx, MySQL, and PHP-FPM), OpenSearch, and Redis. You have also secured Magento with HTTPS through Certbot and Letsencrypt.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular