Magento 2 is a popular open-source eCommerce platform that offers flexibility and scalability. To optimize its performance, you can configure Magento 2 with Varnish cache and Apache on Ubuntu 16.04. Whether you're hosting your eCommerce site on a Windows VPS UK or another cloud service, this setup will improve site speed and ensure efficient resource management.
Prerequisites
Before starting, ensure you have the following:
- An Ubuntu 16.04 server, which could be hosted on a UK Windows VPS, Windows Virtual Private Servers, or another Windows VPS Hosting UK solution.
- Root or sudo privileges on your server.
- Basic understanding of Linux and server management.
Step 1: Update Your System
Before installing any packages, make sure your system is up to date:
sudo apt update && sudo apt upgrade -y
Step 2: Install Apache Web Server
First, install Apache, which will serve as the web server for Magento:
sudo apt install apache2 -y
Start and enable Apache to run at boot:
sudo systemctl start apache2
sudo systemctl enable apache2
Step 3: Install PHP and Required Extensions
Magento 2 requires PHP and specific PHP extensions. Install PHP 7.0 and the necessary extensions with the following command:
sudo apt install php7.0 libapache2-mod-php7.0 php7.0-mysql php7.0-xml php7.0-curl php7.0-intl php7.0-zip php7.0-mbstring php7.0-bcmath php7.0-gd php7.0-soap -y
Step 4: Install and Configure MariaDB
Magento requires a database to store its data. Install MariaDB with the following command:
sudo apt install mariadb-server -y
After installation, secure your MariaDB setup:
sudo mysql_secure_installation
Log in to the MariaDB shell and create a database and user for Magento:
sudo mysql -u root -p
CREATE DATABASE magento2;
CREATE USER 'magento_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON magento2.* TO 'magento_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 5: Install Magento 2
Navigate to the web root directory and download Magento 2:
cd /var/www/html
wget https://github.com/magento/magento2/archive/2.4.2.zip
Extract the downloaded file:
sudo apt install unzip
unzip 2.4.2.zip
Move the extracted files to a directory named "magento" and set the correct permissions:
sudo mv magento2-2.4.2/ magento
sudo chown -R www-data:www-data /var/www/html/magento
sudo chmod -R 755 /var/www/html/magento
Step 6: Configure Apache for Magento
Create a new Apache virtual host configuration file for Magento:
sudo nano /etc/apache2/sites-available/magento.conf
Add the following configuration:
<VirtualHost *:80>
ServerAdmin admin@your-domain.com
DocumentRoot /var/www/html/magento
ServerName your-domain.com
<Directory /var/www/html/magento>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file, then enable the virtual host and Apache rewrite module:
sudo a2ensite magento.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
Step 7: Install and Configure Varnish Cache
Varnish is a caching solution that will speed up Magento 2 by caching content and reducing server load. Install Varnish with the following command:
sudo apt install varnish -y
Configure Varnish to listen on port 80 by editing the Varnish default configuration:
sudo nano /etc/default/varnish
Find the DAEMON_OPTS
line and update the port configuration:
DAEMON_OPTS="-a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m"
Edit the Apache configuration file to change Apache's port to 8080:
sudo nano /etc/apache2/ports.conf
Replace Listen 80
with:
Listen 8080
Also, update the Magento virtual host file to listen on port 8080:
sudo nano /etc/apache2/sites-available/magento.conf
Replace <VirtualHost *:80>
with:
<VirtualHost *:8080>
Restart Apache and Varnish:
sudo systemctl restart apache2
sudo systemctl restart varnish
Step 8: Configure Magento 2 for Varnish
Magento 2 supports Varnish out of the box. To enable Varnish, navigate to the Magento admin panel and go to Stores > Configuration > Advanced > System. Under Full Page Cache, select Varnish Cache and configure the Varnish settings.
Conclusion
By following these steps, you have successfully installed and configured Magento 2 with Varnish and Apache on Ubuntu 16.04. This setup will significantly improve the performance of your eCommerce website. Whether you're hosting your site on a Windows VPS UK, Windows VPS Italy, or another Windows Virtual Private Server Hosting solution, Magento 2 with Varnish ensures fast page loading times and better user experience.