Magento 2 is a powerful eCommerce platform, and combining it with Varnish and Apache can significantly enhance your website’s performance. This guide provides a step-by-step approach to setting up Magento 2 with Varnish and Apache on Ubuntu 16.04.
📋 Prerequisites
- A server running Ubuntu 16.04.
- Root or sudo access to the server.
- A domain name pointed to your server’s IP address.
🔧 Step 1: Update System Packages
Begin by updating your system’s package index and upgrading existing packages:
sudo apt-get update -y
sudo apt-get upgrade -y
🧰 Step 2: Install Apache and Varnish
Install Apache web server and Varnish cache:
sudo apt-get install apache2 varnish -y
Start and enable Apache to run on boot:
sudo systemctl start apache2
sudo systemctl enable apache2
🐘 Step 3: Install PHP and Required Extensions
Magento 2 requires PHP 7.0 along with several extensions:
sudo apt-get install php7.0 libapache2-mod-php7.0 php7.0-mbstring php7.0-mysql php7.0-mcrypt php7.0-xsl php-imagick php7.0-gd php7.0-cli php-pear php7.0-intl php7.0-curl php7.0-zip php7.0-soap php7.0-xml -y
Modify PHP settings to meet Magento’s requirements:
sudo nano /etc/php/7.0/cli/php.ini
Update the following lines:
memory_limit = 512M
upload_max_filesize = 128M
zlib.output_compression = On
max_execution_time = 1800
Save and exit the file.
🗄️ Step 4: Install and Configure MariaDB
Install MariaDB server:
sudo apt-get install mariadb-server -y
Start and enable MariaDB:
sudo systemctl start mysql
sudo systemctl enable mysql
Secure the MariaDB installation:
sudo mysql_secure_installation
Follow the prompts to set the root password and remove insecure defaults.
Log in to MariaDB to create a database and user for Magento:
mysql -u root -p
Within the MariaDB shell, execute:
CREATE DATABASE magento_db;
CREATE USER 'magento'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON magento_db.* TO 'magento'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Replace ‘your_password’ with a strong password.
📥 Step 5: Download and Set Up Magento 2
Download the latest Magento 2 package from the official website or use Composer. Assuming you’ve downloaded the Magento 2 archive:
sudo mkdir /var/www/html/magento
sudo unzip magento2.zip -d /var/www/html/magento
Set the appropriate permissions:
sudo chown -R www-data:www-data /var/www/html/magento
sudo find /var/www/html/magento -type d -exec chmod 755 {} \;
sudo find /var/www/html/magento -type f -exec chmod 644 {} \;
🌐 Step 6: Configure Apache for Magento
Create a new virtual host configuration for Magento:
sudo nano /etc/apache2/sites-available/magento.conf
Add the following content:
<VirtualHost *:8080>
ServerAdmin [email protected]
DocumentRoot /var/www/html/magento
ServerName yourdomain.com
<Directory /var/www/html/magento>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/magento_error.log
CustomLog ${APACHE_LOG_DIR}/magento_access.log combined
</VirtualHost>
Enable the new site and necessary modules:
sudo a2ensite magento.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
🚀 Step 7: Install and Configure Varnish
Install Varnish:
sudo apt-get install varnish -y
Configure Varnish to listen on port 80 and forward requests to Apache on port 8080. Edit the Varnish default configuration:
sudo nano /etc/varnish/default.vcl
Set the backend to Apache:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Update the Varnish service to listen on port 80:
sudo nano /etc/systemd/system/varnish.service
Modify the ExecStart line:
ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -s malloc,256m
Reload systemd and restart Varnish:
sudo systemctl daemon-reload
sudo systemctl restart varnish
🛠️ Step 8: Configure Magento to Use Varnish
Access the Magento Admin Panel and navigate to:
Stores > Configuration > Advanced > System > Full Page Cache
Set Caching Application to Varnish Cache (Recommended). Expand the Varnish Configuration section and click on Export VCL for Varnish 4. This will generate a default.vcl file.
Replace the existing Varnish configuration with the exported one:
sudo cp /var/www/html/magento/var/default.vcl /etc/varnish/default.vcl
sudo systemctl restart varnish
✅ Step 9: Finalize Magento Installation
Complete the Magento installation via the web setup wizard by navigating to your domain in a browser. Follow the on-screen instructions to finish the setup.
🧪 Step 10: Verify Varnish is Caching
Use the following command to check if Varnish is caching your pages:
curl -I http://yourdomain.com
Look for the X-Varnish and Via headers in the response, indicating that Varnish is active.
🎉 Conclusion
By following this guide, you’ve successfully installed Magento 2 with Varnish and Apache on Ubuntu 16.04. This setup enhances your eCommerce site’s performance by leveraging Varnish’s caching capabilities and Apache’s robust web serving.
Meta Description: Learn how to install Magento 2 with Varnish and Apache on Ubuntu 16.04. This step-by-step guide covers installation, configuration, and optimization for a high-performance eCommerce platform.
Keywords: Magento 2 installation Ubuntu 16.04, Varnish cache Magento 2, Apache Magento setup, Magento 2 performance optimization
Hướng dẫn cài đặt Cluster Kafka trên Ubuntu










Add Comment