Bobcares

How to Migrate Your Website from Apache to Nginx on Ubuntu

PDF Header PDF Footer

Discover how to migrate your website from an Apache Web Server to Nginx on an Ubuntu VPS. Our Apache Support team is ready to assist with any queries or concerns.

How to Migrate Your Website from Apache to Nginx on Ubuntu

Are you considering migrating your website from Apache to Nginx?

Well, you are in luck!

How to Migrate Your Website from Apache to Nginx on Ubuntu

Our experts will guide you through migrating your website from Apache to Nginx on Ubuntu, ensuring minimal downtime and a seamless migration. This tutorial is compatible with various Ubuntu versions, including Ubuntu 24.04.

Step 1. Install Nginx and PHP-FPM

First, we need to install Nginx and set up PHP processing since Nginx doesn’t handle PHP natively. Instead, it delegates this to a service like php-fpm (FastCGI Process Manager).

To install Nginx:


sudo apt-get update
sudo apt-get install nginx
Copy Code

To install PHP-FPM:

sudo apt-get install php5-fpmCopy Code

This installation gives us all the tools we need to start serving the website with Nginx.

If you’re managing websites using DirectAdmin, you might also be interested in changing Apache to Nginx in DirectAdmin.

Step 2. Set Up a Test Nginx Configuration

To avoid downtime, it’s best to configure Nginx to run alongside Apache during the transition. We can do this by making Nginx listen on a different port, like 8000, while Apache continues to use port 80.

To modify the Nginx default site configuration:

sudo nano /etc/nginx/sites-available/defaultCopy Code

Then, find the `server` block and add:


server {
listen 8000;
...
}
Copy Code

To start Nginx:

sudo service nginx startCopy Code

Now visit:

http://your_ip_or_domain:8000Copy Code

The Apache instance should still be accessible at:

http://your_ip_or_domainCopy Code

For advanced users looking to configure multiple virtual hosts in Nginx, check out this detailed Nginx vhost configuration guide for Ubuntu.

Step 3. Translate Apache Configuration to Nginx

Apache uses Virtual Hosts located in `/etc/apache2/sites-available/`. In Nginx, the equivalent is called Server Blocks, and they live in `/etc/nginx/sites-available/`.

We need to translate each virtual host into an Nginx server block manually.

Here are some key differences to note:

  • Nginx manages error logging globally in `/etc/nginx/nginx.conf`, so you can omit individual `error_log` directives.
  • The ServerAdmin Apache directive is not used in Nginx.
  • Nginx sends PHP requests to `php-fpm` using a socket.
  • Nginx does not support `.htaccess`. You must manually translate any rewrite or access rules into your Nginx configuration.
  • Configure the file priority explicitly with:
    index index.php index.html index.htm;Copy Code

Step 4. Configure PHP-FPM

Next, configure PHP-FPM to safely handle PHP requests.

To edit `php.ini`:

sudo nano /etc/php5/fpm/php.iniCopy Code

Then, find:

cgi.fix_pathinfo=1Copy Code

Now, change it to:

cgi.fix_pathinfo=0Copy Code

This prevents PHP from attempting to guess file paths, improving security.

To configure the PHP-FPM socket:

sudo nano /etc/php5/fpm/pool.d/www.confCopy Code

Then, find the `listen` directive and set:

listen = /var/run/php5-fpm.sockCopy Code

If we need to handle heavy traffic, adjust:

pm.max_children = [number]Copy Code

To restart services:


sudo service php5-fpm restart
sudo service nginx restart
Copy Code

We can test PHP by creating a file like `info.php` in the web root:

<?php phpinfo(); ?>Copy Code

Now go to http://your_ip_or_domain:8000/info.php, and we will see `nginx` listed under `SERVER_SOFTWARE`.

Step 5. Go Live with Nginx

After thorough testing, we are ready to switch your site to use Nginx on port 80.

To update the server block:

sudo nano /etc/nginx/sites-available/defaultCopy Code

Now, change:

listen 8000;Copy Code

To:

listen 80;Copy Code

To handle Apache conflicts, disable its virtual hosts or stop the Apache service if it is still running on port 80.

sudo service apache2 stopCopy Code

Or, if we are still using Apache for other sites, adjust its configuration files:

  • /etc/apache2/ports.conf
  • /etc/apache2/apache2.conf
  • /etc/apache2/sites-enabled/

To reload services:

sudo service nginx reloadCopy Code

To fully remove Apache:


sudo apt-get remove apache2 apache2-utils apache2.2-bin apache2.2-common libapache2-mod-php5
sudo apt-get autoremove
Copy Code

Step 6. Watch for Migration Complications

Unlike Apache, Nginx doesn’t allow directory-level configuration files like `.htaccess`. So, all rules must be placed inside the Nginx configuration itself. For URL rewrites, use Nginx’s `rewrite` directive, but be aware that its syntax differs from Apache’s `mod_rewrite`.

Furthermore, Apache uses modules like `dir` to define default index files. In Nginx, we must declare this explicitly:

index index.php index.html index.htm;Copy Code

If the Apache configuration is complex, consider compiling all configurations into one document and translating them systematically to avoid missing critical behaviors.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

Migrating from Apache to Nginx can improve your site’s performance and scalability. By setting up Nginx alongside Apache first, translating configurations carefully, and testing thoroughly, we can minimize downtime and ensure a smooth transition.

In short, our Support Engineers demonstrated how to migrate your website from an Apache Web Server to Nginx on an Ubuntu VPS.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Get featured on the Bobcares blog and share your expertise with a global tech audience.

WRITE FOR US
server management

Spend time on your business, not on your servers.

TALK TO US

Or click here to learn more.

Speed issues driving customers away?
We’ve got your back!