Magento is a powerful and widely-used open-source eCommerce platform that allows programmers to create robust and customizable online stores. If you’re looking to set up your own eCommerce website, Magento is an excellent choice. In this guide, we will walk you through the step-by-step process of installing Magento on Ubuntu 22.04, using Apache as the web server and Let’s Encrypt SSL for secure communication.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- A server running Ubuntu 22.04.
- A valid domain name pointed to your server’s IP address.
- A root password configured on your server.
Step 1: Install Apache, MySQL, and PHP
The first step is to install the Apache web server and MySQL database server on your Ubuntu 22.04 server. Open a terminal and run the following command:
sudo apt install apache2 mysql-server -y
Next, we need to install PHP 7.4, as Magento supports only PHP versions 7.3 to 7.4. By default, Ubuntu 22.04 provides PHP 8.1 as the default version. To install PHP 7.4, we will use the PHP Ondrej repository. Run the following commands to add the repository and install PHP:
sudo apt install software-properties-common -y sudo add-apt-repository ppa:ondrej/php
apt update -y apt install php7.4 libapache2-mod-php7.4 php7.4-common php7.4-gmp php7.4-curl php7.4-soap php7.4-bcmath php7.4-intl php7.4-mbstring php7.4-xmlrpc php7.4-mysql php7.4-gd php7.4-xml php7.4-cli php7.4-zip -y
After installing PHP 7.4, we need to make some changes to the PHP configuration file. Open the file using a text editor:
sudo nano /etc/php/7.4/apache2/php.ini
Inside the file, search for the following values and make the necessary changes:
file_uploads = On allow_url_fopen = On short_open_tag = On memory_limit = 512M upload_max_filesize = 128M max_execution_time = 3600
Save and close the file. Then, restart the Apache service to apply the configuration changes:
sudo systemctl restart apache2
Step 2: Create a Database for Magento
Magento uses MySQL as its database backend, so we need to create a database and a user for Magento. Open a terminal and log in to the MySQL shell:
mysql
Once you are connected to the MySQL shell, run the following commands to create a database and user for Magento:
CREATE DATABASE magento2;
CREATE USER 'magento2'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON magento2.* TO 'magento2'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 3: Install Composer
Composer is a dependency manager for PHP, and we need it to install the PHP dependencies required by Magento. Start by installing the Curl command-line tool:
sudo apt-get install curl -y
Next, use Curl to install Composer:
curl -sS https://getcomposer.org/installer -o composer-setup.php sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
To verify that Composer is installed correctly, run the following command:
composer --version
Step 4: Download and Install Magento
Now that we have all the prerequisites in place, it’s time to download and install Magento. We will use Composer to download Magento version 2.4.3. Before we proceed, you need to create an access key on the Magento website. Visit the Magento Marketplace and create an access key.
Once you have the access key, open a terminal and run the following commands:
cd /var/www/html composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.4.3 magento2
During the installation process, you will be prompted to enter your access key. Provide the access key when prompted.
Once the installation is complete, set the proper ownership and permissions for the Magento directory:
chown -R www-data:www-data /var/www/html/magento2/
cd /var/www/html/magento2
find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
chown -R :www-data .
chmod u+x bin/magento
Next, run the following command to install Magento:
bin/magento setup:install --base-url=http://magento.example.com --db-host=localhost --db-name=magento2 --db-user=magento2 --db-password=password --admin-firstname=Hitesh --admin-lastname=Jethva --admin-email=admin@example.com --admin-user=admin --admin-password=Magento@Secure1Password --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1
During the installation process, you may encounter an error related to Elasticsearch. To resolve this error, run the following command:
php bin/magento module:disable {Magento_Elasticsearch,Magento_InventoryElasticsearch,Magento_Elasticsearch6,Magento_Elasticsearch7}
You will also need to disable two-factor authentication by running the following command:
sudo -u www-data bin/magento module:disable Magento_TwoFactorAuth
After disabling the necessary modules, run the Magento installation command again:
bin/magento setup:install --base-url=http://magento.example.com --db-host=localhost --db-name=magento2 --db-user=magento2 --db-password=password --admin-firstname=Hitesh --admin-lastname=Jethva --admin-email=admin@example.com --admin-user=admin --admin-password=Magento@Secure1Password --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1
Once the installation is complete, clear the cache and create a Magento cron job:
sudo -u www-data bin/magento cache:flush sudo -u www-data bin/magento cron:install
Step 5: Configure Apache for Magento
To serve Magento over the web, we need to configure Apache with a virtual host. Open a terminal and create a new Apache configuration file:
sudo nano /etc/apache2/sites-available/magento2.conf
Add the following lines to the file:
<VirtualHost *:80> ServerAdmin [email test@example.com] DocumentRoot /var/www/html/magento2/ ServerName magento.example.com <Directory /var/www/html/magento2/> Options FollowSymlinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/magento2_error.log CustomLog ${APACHE_LOG_DIR}/magento2_access.log combined </VirtualHost>
Save and close the file. Then, enable the Magento virtual host and the Apache rewrite module:
sudo a2ensite magento2.conf
sudo a2enmod rewrite
Finally, restart the Apache service to apply the changes:
sudo systemctl restart apache2
Step 6: Access the Magento Web Interface
Now that Magento is installed and configured, you can access the Magento dashboard using your web browser. Open your browser and enter the URL http://magento.example.com/admin_example. You will be redirected to the Magento login page.
Enter your admin username and password, then click on the Login button. You should now see the Magento dashboard, where you can start configuring your online store.
Step 7: Secure Magento with Let’s Encrypt SSL
To secure your Magento website with Let’s Encrypt SSL, you need to install the Certbot client. Open a terminal and run the following command to install Certbot:
sudo apt-get install python3-certbot-apache -y
Once Certbot is installed, run the following command to secure your website with Let’s Encrypt SSL:
sudo certbot --apache -d magento.example.com
During the installation process, you will be asked to provide your email and accept the terms of service. Follow the prompts to complete the installation.
Congratulations! You have successfully installed Magento on Ubuntu 22.04 with Apache and Let’s Encrypt SSL. Your online store is now ready to be customized and launched. If you have any questions or run into any issues, feel free to reach out for assistance.
And if you’re looking for reliable and secure hosting services for your Magento website, consider checking out Shape.host. They offer scalable Linux VPS solutions with SSD storage, ensuring optimal performance and stability for your eCommerce store. Start your journey to online success with Shape.host today!