How to use Configuration Templates
Introduction
If you are using OpenLiteSpeed, LiteSpeed Web ADC, or native LiteSpeed Web Server Enterprise without a control panel, you can take advantage of virtual host configuration templates.
Today we’ll show you how templates work, go over some benefits, and walk you through an example usage.
Benefits of Templating
When you host multiple websites on one server, you may find that many of them have similar configurations. With LiteSpeed’s virtual host template feature, you can create many virtual hosts from one master template and edit all of their configurations at once. Plus, configuring a new vhost can be as simple as connecting it to the template.
How Configuration Templates Work
Each template contains three things:
- a configuration file
- a list of mapped listeners, and
- a list of member virtual hosts.
Creating a Template
To create a brand new template, navigate to WebAdmin Console > Configuration > Virtual Host Templates > Add. You will be asked to fill out basic information for this template. One of the fields will require the location of your template file. If you do not have a template file yet, that’s fine. You can make up a new file name and LiteSpeed will create it for you in $SERVER_ROOT/conf/templates/
.
For the purposes of this blog post, let’s call your new template mytemplate
.
Once you’ve added mytemplate
, you don’t need to configure any more settings, though you will probably want to.
Most of the settings in a virtual host template are comparable to normal virtual host settings. The main difference is that you can use the $VH_NAME
variable in template settings to stand in for the names of the virtual hosts that you will be applying the template to.
Applying a Template to a Virtual Host
A virtual host template does nothing until you assign it to some member virtual hosts.
Virtual host template settings are located in the WebAdmin Console under Configuration > Virtual Host Templates. That is where you can find a list of your existing templates.
To add virtual hosts to mytemplate, click mytemplate and navigate to Template > Member Virtual Hosts > Add. Each virtual host you add will be automatically configured with the mytemplate settings. The member virtual host’s name will be substituted anywhere that $VH_NAME
appears in the template settings. And listener-to-virtual host mapping will be added automatically for all listeners in the template’s mapped listener list.
Making Changes to a Template
In the WebAdmin Console, navigate to Configuration > Virtual Host Templates, and click View/Edit next to mytemplate, (or whichever template you wish to edit).
When you update the settings in a virtual host template, all of the member virtual hosts will automatically take on the new settings.
You will need to perform a Graceful Restart to put your updates into effect.
Default Templates
Our server products may come with some default virtual host templates, which can be found in the $SERVER_ROOT/conf/templates/
directory.
LiteSpeed Web Server has three, for the three most common types of virtual hosts:
centralConfigLog
is good for basic virtual hosts that will use the same server root directories and share a common directory naming framework.PHP_SuEXEC
contains a setup ideal for hosting external PHP applications running in suEXEC mode. PHP scripts for member virtual hosts will be executed as the owner of the virtual host’s document root.EasyRailsWithSuEXEC
is just like thePHP_SuEXEC
template, but for Ruby on Rails applications.
Example
Let’s set up a new template in the LiteSpeed WebAdmin Console.
- Click Configuration > Listeners and delete the default port 80 listener.
- Click Add. Set the following, and click Save:
- Listener name:
http
- Port:
80
- Binding
Not set
- Listener name:
- Repeat step 2 to create an
https
listener for port 443. - Configure an SSL certificate for the
https
listener. - Click the Virtual Host Templates tab and click Add. Set the following, and click Save:
- Template Name:
mytemplate
(or whatever name you would like to use) - Template file:
/usr/local/lsws/conf/templates/mytemplate.xml
- Mapped Listeners:
http, https
- Template Name:
- Click View/Edit on
mytemplate
. Click the General tab, and click Edit. Set the following, and click Save:- Default Virtual Host Root:
/home/$VH_NAME/
- Config file:
/usr/local/lsws/conf/vhosts/$VH_NAME.xml
- Document Root:
/home/$VH_NAME/public_html
- Default Virtual Host Root:
- Click the Security tab. Set the following, and click Save:
- External App Set UID Mode:
DocRoot UID
- suEXEC User:
$VH_NAME
- suEXEC Group:
$VH_NAME
- External App Set UID Mode:
- Click the External App tab and choose the LiteApeed SAPI App type. Set the following, and click Save:
- Name:
$VH_NAME_lsphp81
(we are using PHP v8.1 in this example, but you can use any version you like) - Address:
uds://tmp/lshttpd/$VH_NAME.sock
- Max Connections:
3
- Initial Request Timeout (secs):
60
- Retry Timeout (secs):
30
- Connection Keepalive Timeout:
-1
- Command:
/usr/local/lsws/lsphp81/bin/lsphp
- Run as user:
$VH_NAME
- Run as group:
$VH_NAME
- Name:
- Click the Script Handler tab and click Add. Set the following, and click Save:
- Suffixes:
php
- Handler type:
LiteSpeed SAPI
- Handler name:
[VHost Level]: $VH_NAME_lsphp81
- Suffixes:
- Click the Template tab and click Add. Set the following, and click Save:
- Virtual Host Name:
site1
(or any name of your choice) - Domain:
site1.com
- Aliases:
www.site1.com
- Virtual Host Name:
- Click the Add button again. Set the following, and click Save:
- Virtual Host Name:
site2
(or any name of your choice) - Domain:
site2.com
- Aliases:
www.site2.com
- Virtual Host Name:
- Repeat step 11 for every virtual host that you want to add with this template.
- Click Graceful restart to apply your changes
Testing
Once the template is created and the site1
and site2
virtual hosts are added as members, you can (1) test that the sites exist, (2) check the file system to verify that their document roots are in the correct place, as defined by the template, and (3), verify that each site owns a running PHP processes, as defined by the template.
- In a browser window, verify that you can successfully visit
site1.com
andsite2.com
. - Verify that document roots exist in
/home/site1/public_html
and/home/site2/public_html
. - Run:
ps -aux | grep lsphp
. This command will show you anylsphp
processes running, and you can verify that they are owned bysite1:site1
andsite2:site2
.
Conclusion
You should now know why templates are useful, and you should be able to create one and assign virtual hosts as members.
If you’d like to learn more, you can see our wiki on the subject, or take a look at these instructions for creating a template to be used with LiteSpeed Web ADC and Kubernetes.
Comments