Differences

This shows you the differences between two versions of the page.

Link to this comparison view

advanceduses:installing_lighttpd_php_mysql_on_tonidoplug [2010/03/03 13:54]
gatesphere
advanceduses:installing_lighttpd_php_mysql_on_tonidoplug [2011/12/29 22:10] (current)
Line 3: Line 3:
 Want to turn your plug into a web server?  This guide will show you how to install a lightweight, standards-compliant web server (lighttpd) that supports PHP and MySQL, and is flexible enough to power most web apps. Want to turn your plug into a web server?  This guide will show you how to install a lightweight, standards-compliant web server (lighttpd) that supports PHP and MySQL, and is flexible enough to power most web apps.
  
 +=== Installation ===
 1. SSH into your plug: 1. SSH into your plug:
   $ ssh root@<ipaddress>   $ ssh root@<ipaddress>
Line 21: Line 22:
 5. Install PHP: 5. Install PHP:
 Issue the following command as root to install a FastCGI-enabled version of PHP and MySQL support: Issue the following command as root to install a FastCGI-enabled version of PHP and MySQL support:
-  #apt-get install php5-cgi php5-mysql+  # apt-get install php5-cgi php5-mysql
 You may wish to install other php5 modules at this time as well, if you think they'll be needed by your server.  You can always do this later. You may wish to install other php5 modules at this time as well, if you think they'll be needed by your server.  You can always do this later.
  
 After PHP is installed, open up //''/etc/php5/cgi/php.ini''// in your favorite text editor (I prefer ''joe'', which is available with a simple ''apt-get install joe'') and add the following line to the bottom of the file: After PHP is installed, open up //''/etc/php5/cgi/php.ini''// in your favorite text editor (I prefer ''joe'', which is available with a simple ''apt-get install joe'') and add the following line to the bottom of the file:
   cgi.fix_pathinfo = 1   cgi.fix_pathinfo = 1
 +
 +To allow PHP to work, you have to make sure that the ''/tmp'' directory on your system is world-writable, which the TonidoPlug has as root writable only by default. So, as root, simply issue the command
 +  # chmod 777 /tmp
 +and this should make ''/tmp'' world-writable.
  
 6. Enable PHP support in lighttpd: 6. Enable PHP support in lighttpd:
Line 33: Line 38:
   # /etc/init.d/lighttpd restart   # /etc/init.d/lighttpd restart
 If all goes well, you should now have a server with MySQL and PHP support.  You may wish to test this by placing a //''info.php''// file in your //''/var/www''// directory with the following contents: If all goes well, you should now have a server with MySQL and PHP support.  You may wish to test this by placing a //''info.php''// file in your //''/var/www''// directory with the following contents:
-  <?php +<file php info.php> 
-  phpinfo(); +<?php 
-  ?>+phpinfo(); 
 +?
 +</file>
 Access the file using a web browser pointed to //''<ipaddress>/info.php''//, where //''<ipaddress>''// is the internal IP address of your TonidoPlug.  You should see a large listing of various PHP settings, with a section called MySQL. Access the file using a web browser pointed to //''<ipaddress>/info.php''//, where //''<ipaddress>''// is the internal IP address of your TonidoPlug.  You should see a large listing of various PHP settings, with a section called MySQL.
  
 Congratulations, you now have a fully working PHP/MySQL web server!  Be sure to forward port 80 on your router to your TonidoPlug if you intend to use this to host a website. Congratulations, you now have a fully working PHP/MySQL web server!  Be sure to forward port 80 on your router to your TonidoPlug if you intend to use this to host a website.
 +
 +
 +=== Optional Configuration ===
 +Below are some optional configuration options to help optimize your server. After each of these, be sure to restart lighttpd with a simple
 +  # /etc/init.d/lighttpd restart
 +to allow the server to accept the changes.
 +
 +== Using your own domain ==
 +If you own a domain that you wish to use, make sure port 80 is forwarded on your router, and that the ''@'' and ''*'' records in your DNS entries point to your external IP address.  This should allow you to connect to your site from your domain.
 +
 +== Simple V-hosts ==
 +If you want to use multiple different domains or subdomains on your server to load different pages, add a section like the following in to your //''/etc/lighttpd/lighttpd.conf''// file:
 +  ## vhost1
 +  $HTTP["host"] =~ "(^|\.)subdomain\.example\.org$" {
 +        server.document-root = "/var/www/example.org/subdomain/public_html/"
 +        accesslog.filename = "/var/www/logs/subdomain.access.log"
 +  }
 +  ## vhost2
 +  else $HTTP["host"] =~ "(^|\.)anothersub\.example\.org$" {
 +        server.document-root = "/var/www/example.org/anothersub/public_html/"
 +        accesslog.filename = "/var/www/logs/anothersub.access.log"
 +  }
 +In this example, someone who went to ''subdomain.example.org'' would be sent the documents in //''/var/www/example.org/subdomain/public_html/''//, and would be logged in the access file //''/var/www/logs/subdomain.access.log''//.  If someone went to //''anothersub.example.org''// however, they'd be sent to whatever site is within //''/var/www/example.org/anothersub/public_html/''// and be logged to the file //''/var/www/logs/anothersub.access.log''//.  Note that the string to match is a regular expression, and thus as presented, if a user requests a subdomain of one of these subdomains, something like //''foo.anothersub.example.org''//, they'd be sent to //''anothersub.example.org''//.  Special cases like this are left to the reader to figure out.
 +
 +== Using compression and caching to increase performance and reduce bandwidth ==
 +All modern web browsers are able to decompress pages sent with various compression algorithms.  This not only increases download speed (it's a much smaller file to deliver), but also decreases bandwidth usage.  It's a win-win.  Along with compression, there's also caching, which tells the browser not to re-download commonly used, rarely modified parts of a page, such as CSS or static image files.  Lighttpd has support for both of these faculties, just as any decent web server should.
 +
 +To enable compression, open your //''/etc/lighttpd/lighttpd.conf''// file in your favorite text editor, and find the //''server.modules''// section at the top, and make sure that the line that says //''mod_compress,''// is uncommented.
 +
 +Now, a little further down in the file, before your v-hosts (if applicable), add the following sections.
 +  ## mimetype mapping
 +  include_shell "/usr/share/lighttpd/create-mime.assign.pl"
 +and
 +  ## compress module
 +  compress.allowed-encodings = ("bzip2", "gzip", "deflate")
 +  compress.cache-dir          = "/var/cache/lighttpd/compress/"
 +  compress.filetype           = ("text/plain", "text/html", "application/x-javascript", "text/css", "text/javascript", "text/xml")
 +This should now compress all text, html, css, xml, and javascript files sent out by your server.
 +
 +To enable caching, go back to the //''server.modules''// section in the conf file, and make sure that //''mod_expire,''// is uncommented and placed before //''mod_compress,''//.  Then add the following lines to the conf file, above your v-hosts.
 +  ## etags
 +  $HTTP["url"] =~ "(css|js|png|jpg|ico|gif)$" {
 +        expire.url = ( "" => "access 7 days" )
 +  }
 +  etag.use-inode = "enable"
 +  etag.use-mtime = "enable"
 +  etag.use-size = "enable"
 +  static-file.etags = "enable"
 +This will now tell the browser to cache all css/js/png/jpg/ico/gif files for 7 days, unless modified on the server between requests.  The server will check if the file has been modified based on the inode of the file, the modified time of the file, or the size of the file.
 +