how to configure nginx with magento on ubuntu localhost

how to configure nginx with magento on ubuntu localhost

October 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to configure nginx with magento on ubuntu localhost

Configuring Nginx for Magento on Ubuntu Localhost

If you’re looking to install both Magento 1.8 and 1.9 on an Ubuntu server running Nginx, you’ve come to the right place. In this guide, we’ll walk through setting up a global configuration file that works for both versions of Magento.

Step-by-Step Configuration Process

Firstly, let’s understand why your current configuration isn’t working as expected:

server {
    listen 80 default_server;
    # ... rest of the config ...
}

The issue lies in the default_server directive. This means that if no other server block matches the request, this one will be used by default. In a production environment, this is likely causing conflicts with other websites or applications on your server.

For local development purposes, let’s create a more specific configuration:

A More Suitable Configuration

Create a new file in /etc/nginx/sites-available/magento.conf:

# /etc/nginx/sites-available/magento.conf

server {
    listen 80;
    server_name localhost;

    root /var/www/html/magento;   # Magento root directory
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME  $realpath_root$fastcgi_script_name;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }
}

Several things to note in this configuration:

  • Replace /var/www/html/magento with the actual path where you’ve installed Magento.
  • The try_files directive passes non-PHP files directly to the browser and routes PHP requests to index.php. This is necessary for Magento’s core functionality.
  • We’re using the PHP-FPM socket (/var/run/php-fpm.sock) which assumes that you have PHP-FPM installed and running.
  • We’ve explicitly set SCRIPT_FILENAME to ensure it points correctly to your Magento root directory.

Enabling the New Configuration

After creating this file, enable the new site:

sudo ln -s /etc/nginx/sites-available/magento.conf /etc/nginx/sites-enabled/

Then, restart Nginx to apply the changes:

sudo systemctl restart nginx

Additional Best Practices

To ensure optimal performance and security for your local Magento development environment, consider these best practices:

  1. Use a separate database for each version of Magento.
  2. Set up proper ownership permissions on the Magento directories: chown -R www-data:www-data /var/www/html/magento*.
  3. Configure PHP-FPM settings based on your system’s resources and the specific requirements of each Magento version.

Conclusion

In this post, we’ve demonstrated how to create a more suitable Nginx configuration for local development with both Magento 1.8 and 1.9. By following these steps, you should now have a stable environment for testing and developing your e-commerce applications.

For further reading on optimizing your Magento performance in different environments, check out our article Optimizing Magento Performance: A Comprehensive Guide.

Remember that local development configurations are meant to be simple and efficient. As you move to a production environment, you’ll need to consider additional factors like security, scalability, and traffic management.

Happy coding!

Magento 1 Catalog Indexing Database

Our network