Setting Up NGINX and Apache Virtual Host Configuration Files

Setting Up NGINX and Apache Virtual Host Configuration Files - Plesk

Plesk for Linux uses the Apache web server for website hosting. By default, NGINX makes it stronger so as to achieve a higher level of performance. The Apache web server doesn’t directly operate hosted websites – but it does manages virtual hosts. So what are Virtual hosts? Web resources with an assigned host or IP. Hence, as soon as the admin creates a site under Plesk, the system adds a new virtual host to Apache. So let’s talk about NGINX and Apache virtual host configuration files.

In summary, the configuration settings for each virtual host are stored in configuration files which you’ll find in the /var/www/vhosts/system/<domain_name>/conf/ directory. The ones you’re looking for are:

  • <version>_httpd.conf – Apache virtual host configuration
  • <version>_nginx.conf – NGINX configuration

The final Apache virtual host configuration will include all httpd.conf and nginx.conf files from all virtual hosts.

Where to Find Virtual Host Configuration Files

Plesk always remembers how it was set up before. Because we record all previous configuration states in a virtual host configuration file. Therefore, if you’re an administrator or domain owner, you may want to check what changed and when. Alas, you can certainly find details of current file states here – last_httpd.conf and last_nginx.conf.

Every time you make a change in a virtual hosting configuration, like domain hosting type, the system regenerates the config files. So, while editing httpd.conf and nginx.conf files yourself, changes will be lost after changing virtual host settings in Plesk UI. To prevent this, use extra files to outline custom configuration for domains:

  • vhost.conf and vhost_ssl.conf – custom Apache directives for two situations. First, when clients access the site over HTTP and second, HTTPS. These files go into httpd.conf.
  • vhost_nginx.conf – custom NGINX directives. This file goes into nginx.conf.

Ultimately, you’ll find the files with custom per-domain configuration in the /var/www/vhosts/system/<domain_name>/conf/ directory.

Notice that a lot of the settings in these files take precedence over the server-wide configuration of a virtual host (httpd.conf and nginx.conf). For instance, you may include directives which already exist in the site’s current httpd.conf file. In this case, the system will use your values from the vhost.conf and vhost_ssl.conf files.

Editing Virtual Host Configuration Files

Editing Virtual Host Configuration Files - Plesk

When you want to edit virtual host configuration files, you have a couple of choices:

Manually

You can add custom directives to the following files from /var/www/vhosts/system/<domain_name>/conf/ directory:

  • vhost.conf and vhost_ssl.conf
  • vhost_nginx.conf

Using Plesk GUI

Go to Websites & Domains > select a website > Web Server Settings tab you can specify:

  • Common Apache settings: Most commonly used directives (like MIME types or index files). These directives will be included in httpd.conf.
  • NGINX settings: Directives that define the scope of NGINX’ role in serving different types of website content (static and dynamic).
  • Extra Custom Apache directives: Add several simultaneously in the Additional directives for HTTP and Additional directives for HTTPS fields.
    These fields correspond to vhost.conf and vhost_ssl.conf files respectively.
  • Additional NGINX directives: You can add several custom NGINX directives at once in the Additional NGINX directives field.
    This field corresponds to vhost_nginx.conf file.

The changes you made in the web server configuration will apply automatically.

Just upgraded from an older Plesk version?

If you did and are now configuring vhost.conf and vhost_ssl.conf files,  you need to use the httpdmng utility. That way you can apply changes from your config files. For example, to generate web server configuration files for a website, run the command: /usr/local/psa/admin/sbin/httpdmng –reconfigure-domain <domain_name>

Website Directory Structure – How To Get The Most From Yours With Plesk

Website Directory Structure – How To Get The Most From Yours With Plesk

When you create a website, Plesk Onyx adds a new virtual host to the web server and creates the site’s directory structure too, filling each of them with essential content. These directories sit in the corresponding virtual host directories:

  • On Linux: /var/www/vhosts/<domain_name>
  • On Windows: C:\inetpub\vhosts\<domain_name>

<domain_name> is where the website’s domain name goes. The directory structure is defined by the default virtual host template

If you want to change the files and directories included in new sites, like for instance, you want to add some scripts or make changes to the error pages, you can define a custom virtual host template. Resellers can also customize virtual host templates for their customers.

Note: Subdomains have equal status with domains and employ the same website directory structure. So, they have a separate directory in /var/www/vhosts and their own configuration files, like php.ini or vhost.conf.

Defining a custom document root in the directory structure

Plesk sets the httpdocs directory as the Document Root directory for all hosted domains by default. But you will find that a lot of current web development frameworks will expect that a different directory, such as, <app_root>/public will be used as the document root. But, using Plesk you can change the default Document Root directory by altering the panel.ini file like this:

[domainManagement]

docroot = "new_default_docroot"

where <new_default_docroot> is the name of a directory or a directory path (e.g. “new_docroot” or “<domain>/public”).

You can only use these characters in the name of the Custom Document Root:

  • Slash (/)
  • Period (.)
  • Hyphen (-)
  • Word characters:
    • Alphabet letters A-Z (upper or lower case)
    • 0 to 9 digits
    • Underscore (_)

Note that the changes will apply to all newly created domains, but not to any previously existing domains. The path is relative to the webspace root, and you can use the <domain> environment variable that is replaced with the domain’s name on hosting creation.

Creating a custom virtual host template

Creating a custom virtual host template is straightforward. Just create a directory on your local file system, then add one or more of the following directories inside it:

  • cgi-bin so you can include custom scripts in the template.
  • httpdocs if you would like to include custom documents such as web pages or images.
  • error_docs if you want to include custom error messages.
  • webspace to include custom files that will be placed in the subscription home directory (for example, .bashrc, .vimrc, etc.)

After you have created the directories, put the files like to see included in the newly created domains’ virtual host structure in the corresponding directories. You can use the default files stored in the /var/www/vhosts/.skel/0 on Linux or C:\inetpub\vhosts\.skel\0 on Windows if you wish.

You can also use variables in the custom virtual host template. These will be substituted with the corresponding values after the domain creation:

  • @do[email protected] – Use the domain name in place of this variable. Used on Linux and Windows.
  • @[email protected] – Use the customer’s email address in place of this variable. Used on Windows only.

When you’ve added all your files, put the directories in a tgz, tar, tar.gz, or a zip archive. Take care that the directories are placed in the root of the archive file and not in a sub-directory. If you include any other directories or files in the root of the archive, Plesk won’t add them to the template for the directory structure.

To upload your custom virtual host template, visit Tools & Settings > Virtual Host Template (in the Tools & Resources group), click Browse, locate the prepared archive file, and then click Send File. To revert to the default virtual host template, click Default.