How To Install Magento 2 On Ubuntu 22.04

At Farbyte we offer unmanaged & managed Magento 2 hosting.

If you're using our managed Magento VPS hosting service, we'll install Magento for you, however, if you use our unmanaged VPS, you'll need to install Magento 2 yourself.

This guide will help you through the installation.

What is Magento 2?

Magento 2 is an open-source e-commerce platform written in PHP.

It is used by businesses to create and manage online stores and shopping carts.

Magento 2 offers a variety of features and tools for creating and customising an online store, including support for multiple payment gateways, shipping methods, and multiple languages and currencies.

Installing Magento 2 on a Ubuntu 22.04 server may be useful for users who want to create and manage their own online store.

The platform's customisable features and tools can help businesses to create a unique and user-friendly shopping experience for their customers.

Additionally, the ability to manage the store from a central administration panel can save time and effort for store owners.

Overview of installing Magento 2 on Ubuntu 22.04

To successfully install Magento 2 on a Ubuntu 22.04 server, the following requirements must be met:

  • The server must be running Ubuntu 22.04
  • A web server (such as Apache or Nginx) must be installed and running on the server
  • PHP 7.4 or higher must be installed and configured on the server
  • A database (such as MySQL or MariaDB) must be installed and configured on the server
  • Composer, a dependency manager for PHP, must be installed on the server
  • Additionally, the server should have sufficient resources (such as CPU, memory, and disk space) to run Magento 2 and support the desired number of users and store products.

1. Install Apache, PHP MySQL & Composer

To install Apache, PHP, MySQL, and Composer on Ubuntu 22.04, you can follow these steps:

Update the package index:

sudo apt update

Install Apache:

sudo apt install apache2

Install PHP and some necessary PHP extensions:

sudo apt install php php-common php-mysql php-xml php-intl php-mbstring

Install MySQL:

sudo apt install mysql-server

Install Composer:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Once all of the necessary components are installed, you can proceed with installing Magento 2 on your Ubuntu 22.04 server.

It is important to ensure that the components are properly configured and working before attempting to install Magento 2.

2. Download & extract the Magento 2 installation archive

To download the Magento 2 installation archive on Ubuntu 22.04, you can use the wget command to download the archive from the Magento website.

First, navigate to the directory where you want to download the installation archive.

In this case, we'll download it directly to the Ubuntu Apache servers default website document root.

cd /var/www/

Then, use the following wget command to download the archive:

wget https://magento.com/download

This will download the latest version of Magento 2 to the current directory.

You can then extract the contents of the archive using the tar command:

tar -xzf magento-latest.tar.gz

Once the installation files have been moved to the desired location, you can proceed with the next steps of the installation process.

The following command will move all the files & directories in in the 'magento2' directory to the /var/www directory:

mv -f /var/www/magento2/{.,}* /var/www

Now we should remove the empty magent2 directory:

rm -fr /var/www/magento2

3. Set appropriate permissions on the Magento 2 directory

To set appropriate permissions on the Magento 2 directory on Ubuntu 22.04, you can use the chown and chmod commands.

First, use the chown command to set the ownership of the Magento 2 directory to the www-data user and group.

This will allow the web server, in this case Apache, to access and modify the files in the directory:

sudo chown -R www-data:www-data /var/www

Next, use the chmod command to set the permissions on the Magento 2 directory.

Magento 2 requires specific permissions for certain directories and files in order to function properly.

The following chmod command will set the correct permissions:

sudo find /var/www -type d -exec chmod 770 {} \; && sudo find /var/www -type f -exec chmod 660 {} \; && sudo chmod u+x /var/www/bin/magento

After setting the correct permissions, you can proceed with the next steps of the installation process.

It is important to ensure that the permissions are set correctly, as incorrect permissions can cause issues with the functioning of Magento 2.

4. Use Composer to install the required PHP dependencies

To use Composer to install the required PHP dependencies for Magento 2 on Ubuntu 22.04, you can follow these steps:

1. Open a terminal and navigate to the directory where the Magento 2 installation files are located.
For example, above we installed Magento 2 in the /var/www directory, so you would use the following command:

cd /var/www

2. Use the "composer install" command to install the required PHP dependencies for Magento 2. This command will download and install the necessary dependencies from the Magento 2 repository on Packagist:

composer install

3. Wait for the dependencies to be installed.
This may take some time, depending on the speed of your internet connection and the size of the dependencies being installed.

4. After the dependencies have been installed, you can proceed with the next steps of the installation process.

5. Create a new MySQL database for Magento 2

To create a new MySQL database for Magento 2 on Ubuntu 22.04, follow these steps:

1. Open a terminal and log in to the MySQL command line interface as the root user:

mysql -u root -p

2. Create a new database for Magento 2 by using the CREATE DATABASE statement.
Replace <dbname> with the desired name for the database:

CREATE DATABASE <dbname>;

3. Create a new MySQL user for Magento 2 by using the CREATE USER statement.
Replace <username> and <password> with the desired username and password for the user:

CREATE USER '<username>'@'localhost' IDENTIFIED BY '<password>';

4. Grant the new user permission to access and modify the Magento 2 database by using the GRANT statement.
Replace <dbname> and <username> with the name of the Magento 2 database and the username of the new MySQL user:

GRANT ALL PRIVILEGES ON <dbname>.* TO '<username>'@'localhost';

5. Save the changes and exit the MySQL command line interface by using the FLUSH PRIVILEGES and EXIT statements:

FLUSH PRIVILEGES;
EXIT;

After creating the MySQL database and user for Magento 2, you can proceed with the next steps of the installation process.

6. Run the Magento 2 installation script

To run the Magento 2 installation script on Ubuntu 22.04, you will need to follow these steps:

Open a terminal window and navigate to the directory where you extracted the installation archive.

In our case this is /var/www

cd /var/www

Run the following command to start the installation script:

php bin/magento setup:install

The installation script will ask you to provide the database connection details, as well as your store's base URL and other configuration settings.

Enter the appropriate information and continue through the prompts to complete the installation.

Once the installation is complete, you can access your Magento 2 store by navigating to your store's base URL in a web browser.

7. Verify the Magento 2 installation

To verify that the Magento 2 installation was successful and access the admin panel, follow these steps:

  1. Open a web browser and navigate to your store's base URL, followed by /admin (for example, http://www.example.com/admin).
  2. You will be redirected to the login page for the Magento 2 admin panel.
    Enter your admin username and password, then click the Sign In button to log in.
  3. If the installation was successful, you should see the Magento 2 admin dashboard.
    From here, you can manage your store's settings, products, orders, and other aspects of your online business.

If you are unable to access the admin panel, or if you encounter any errors during the installation process, it is possible that there was an issue with the installation.

In this case, you may need to troubleshoot the problem or try the installation again.

Secure Magento HTTPS Access With An SSL Certificate

You should have a working Magento 2 store at this point.

The next step is to obtain & install a Magento SSL certificate.

After that, you may want to improve the security of your Magento store.

Conclusion

Magento 2 is now running from the default document root on your Ubuntu 22.04 server.

You can now configure DNS settings with your DNS provider to point your web server record to the IP address of your Ubuntu 22.04 server & your Magento 2 website will be displayed. 

More Magento Information

You can find out more about Magento 2 here.