NGINX Unit

WordPress§

To install WordPress if you haven’t already done so:

  1. Check prerequisites and configure the WordPress database.

  2. Download and extract WordPress files:

    # cd /path/to/
    # wget https://wordpress.org/latest.tar.gz
    # tar xzvf latest.tar.gz
    

    In this example, the files will be stored in /path/to/wordpress/.

  3. Update the wp-config.php file with your database settings and other customizations.

  4. Set up proper file permissions for WordPress:

    # chown -R wpuser:www-data /path/to/wordpress/
    # find /path/to/wordpress/ -type d -exec chmod g+s {} \;
    # chmod g+w /path/to/wordpress/wp-content
    # chmod -R g+w /path/to/wordpress/wp-content/themes
    # chmod -R g+w /path/to/wordpress/wp-content/plugins
    

NGINX and Unit§

To run WordPress in Unit:

  1. Install NGINX. Currently, NGINX is required to serve static files.

  2. Install Unit with a PHP language module.

  3. Prepare Unit configuration for your project. To obtain an initial template, query the control API:

    $ curl --unix-socket /path/to/control.unit.sock \
           http://localhost/config/ > config.json
    

    Note

    Control socket path may vary; run unitd --help or see Startup for details.

  4. Edit the file, adding two apps and their listeners to serve direct and indirect WordPress URLs:

    {
        "listeners": {
            "127.0.0.1:8090": {
                "application": "wp_index"
            },
    
            "127.0.0.1:8091": {
                "application": "wp_direct"
            }
        },
    
        "applications": {
            "wp_index": {
                "type": "php",
                "user": "wpuser",
                "group": "www-data",
                "root": "/path/to/wordpress/",
                "script": "index.php"
            },
    
            "wp_direct": {
                "type": "php",
                "user": "wpuser",
                "group": "www-data",
                "root": "/path/to/wordpress/",
                "index": "index.php"
            }
        }
    }
    

    Note

    The difference between the two apps is their usage of script and index settings. Here, wp_index specifies the script that Unit will run for any URL it receives (with WordPress, this is typical of the index.php). The wp_direct app will serve URLs that explicitly specify a PHP file name. This isolates the wp-admin section from the rest of WordPress, allowing to maintain different per-app settings.

  5. Upload the updated configuration:

    # curl -X PUT --data-binary @config.json --unix-socket \
           /path/to/control.unit.sock http://localhost/config
    
  6. Configure NGINX to serve static files and route requests between the apps you have set up in Unit:

    upstream unit_wp_index {
        server 127.0.0.1:8090;
    }
    
    upstream unit_wp_direct {
        server 127.0.0.1:8091;
    }
    
    server {
        listen      80;
        server_name localhost;
        root        /path/to/wordpress/;
    
        location / {
            try_files $uri @index_php;
        }
    
        location @index_php {
            proxy_pass       http://unit_wp_index;
            proxy_set_header Host $host;
        }
    
        location /wp-admin {
            index index.php;
        }
    
        location ~* \.php$ {
            try_files        $uri =404;
            proxy_pass       http://unit_wp_direct;
            proxy_set_header Host $host;
        }
    }
    

    For details, refer to NGINX Admin Guide.

Finally, browse to your WordPress site and complete the installation.

Note

Resulting URL scheme will trickle into your WordPress configuration; updates may require extra steps.

Further Reading§

See a detailed walkthrough in our blog: https://www.nginx.com/blog/installing-wordpress-with-nginx-unit/