Project

General

Profile

TutorialConfiguration » History » Revision 25

Revision 24 (gstrauss, 2020-09-09 16:23) → Revision 25/28 (gstrauss, 2021-07-15 04:20)

h1. Configuration 

 How much time do you have to set up lighttpd? 5 minutes? 10 minutes? More? 


 h2. 5 minutes 

 Want to run a fast, low-resource server for static content? It's easy. Create a text file named lighttpd.conf with the following content: 


 <pre> 
 server.document-root = "/var/www/servers/www.example.org/pages/" 

 server.port = 3000 

 mimetype.assign = ( 
   ".html" => "text/html",  
   ".txt" => "text/plain", 
   ".jpg" => "image/jpeg", 
   ".png" => "image/png" 
 ) 
 </pre> 


 lighttpd will listen on TCP port 3000 and bind to all interfaces by default. The few important MIME types are assigned 
 and the document root (the base directory that is used for all requests) is set. The files in the document root have to be  
 readable by the user starting the web server. 

 First, check that your configuration is ok: 


 <pre> 
 $ lighttpd -t -f lighttpd.conf 
 </pre> 

 Now start the server for testing: 

 <pre> 
 $ lighttpd -D -f lighttpd.conf 
 </pre> 


 and point your browser to http://127.0.0.1:3000/ 

 To stop the server, return to the command prompt and press ctrl-c. 


 h3. A real daemon 

 Next you should familiarize yourself with some settings necessary for your server's security: 

 <pre> 
 server.document-root = "/var/www/servers/www.example.org/pages/" 

 server.port = 80 

 server.username = "www" 
 server.groupname = "www" 

 mimetype.assign = ( 
   ".html" => "text/html",  
   ".txt" => "text/plain", 
   ".jpg" => "image/jpeg", 
   ".png" => "image/png" 
 ) 

 static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" ) 
 index-file.names = ( "index.html" ) 
 </pre> 


 Now the web server is listening on port 80, the default for HTTP traffic, and will switch to the user <code>www</code> and the group <code>www</code>. 
 The server has to be started as root to take control of port 80, but it's not necessary or a good idea to continue running as root after port acquisition, so the server switches to user <code>www</code>. 

 Lastly, access to view the contents of some types of files is forbidden as they are used for generating dynamic content. Requests directly to a directory are rewritten to the <code>index.html</code> file in that directory. 

 Assuming you have already created the <code>/etc/init.d/lighttpd</code> service as described in "Init Script" section of [[InstallFromSource]], place the config file in <code>/etc/lighttpd/lighttpd.conf</code> and start the server with: 

 <pre> 
 # /etc/init.d/lighttpd start 
 </pre> 

 To stop it use: 

 <pre> 
 # /etc/init.d/lighttpd stop 
 </pre> 



 h2. 10 minutes 


 Conditionals, conditionals, conditionals: 

 The most important part in Lighty's configuration is the use of conditionals. Using simple or regular expression conditions, default setting can be overridden. 


 <pre> 
 server.document-root = "/var/www/servers/www.example.org/pages/" 

 server.port = 80 

 server.username = "www" 
 server.groupname = "www" 

 mimetype.assign = ( 
   ".html" => "text/html",  
   ".txt" => "text/plain", 
   ".jpg" => "image/jpeg", 
   ".png" => "image/png" 
 ) 

 static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" ) 
 index-file.names = ( "index.html" ) 

 $HTTP["host"] == "www2.example.org" { 
   server.document-root = "/var/www/servers/www2.example.org/pages/" 
 } 
 </pre> 


 Now we have a new virtual server, <code>www2.example.org</code>, which uses the same settings as the first server, only the document root is different. 

 The following server configuration adds a download area and enables the built-in directory listing feature: 


 <pre> 
 server.document-root = "/var/www/servers/www.example.org/pages/" 

 server.port = 80 

 server.username = "www" 
 server.groupname = "www" 

 mimetype.assign = ( 
   ".html" => "text/html",  
   ".txt" => "text/plain", 
   ".jpg" => "image/jpeg", 
   ".png" => "image/png" 
 ) 

 static-file.exclude-extensions = ( ".fcgi", ".php", ".rb", "~", ".inc" ) 
 index-file.names = ( "index.html" ) 

 $HTTP["host"] == "www2.example.org" { 
   server.document-root = "/var/www/servers/www2.example.org/pages/" 
   $HTTP["url"] =~ "^/download/" { 
     dir-listing.activate = "enable" 
   } 
 } 
 </pre> 


 As you can see, conditionals can be nested: only the <code>download</code> folder and its sub-folders have the directory listings enabled. 

 There's also the <code>else</code> clause for conditionals. Despite the name, it's an <code>else if</code> construct similar to some programming languages, as it has to be followed by another condition.  

 Here's an example of conditional-based vhosts. The <code>else</code> is being used to configure behavior that should be present only in "default" vhost. 

 <pre> 
 $HTTP["host"] == "example.org" {                                                                                          
   # options specific to example.org 
   expire.url = ( "" => "access plus 25 hours" )                                                                                         
 } else $HTTP["host"] == "static.example.org" { 
   # options specific to static.example.org 
   expire.url = ( "" => "access plus 2 weeks" )                                                                                         
 } else $HTTP["host"] =~ "" {                                                                                                            
   # options applied to any other vhosts present on this ip  
   # ie. default options 
   expire.url = ( "" => "access plus 3 hours" )                                                                                         
 }  
 </pre> 

 Now that we've covered the basics, you're ready to learn some more advanced topics like "includes":http://blog.lighttpd.net/articles/2005/11/25/simplify-your-configfiles-with-includes and configuring PHP with [[lighttpd:Docs_ModFastCGI|FastCGI]] 

 h2. After 30 minutes 

 Now you know the basic set up, include files, and maybe even how to set up PHP or Ruby. But there's so much more to find out. 

 Most of it is described in examples in the "default configuration file":https://redmine.lighttpd.net/projects/lighttpd/repository/14/revisions/master/entry/doc/config/lighttpd.conf that you can also find in the <code>doc</code> directory of the tarball. 

 For the *configuration file syntax* and the complete *list of configuration options*, visit the configuration reference section of the wiki: [[WikiStart#Documentation|Documentation]] [[WikiStart#Reference-Documentation|Reference Documentation]] 

 Also check out our [[WikiStart#Community|community]].