You will often see webmasters using Apache2 or Nginx with PHP and MariaDB to create a LAMP or LEMP stack. This post shows you how to use an alternative HTTP server called Lighttpd HTTP server instead.
Lighttpd is a secure, fast, and flexible HTTP server with a small memory footprint compared to other webservers. In addition, its event-driven architecture is optimized for many parallel connections (keep-alive), which is important for high-performance dynamic applications.
Also, if you’re going to be developing any PHP-based application, you’re mostly going to need PHP server scripts installed. PHP is an open-source server scripting language for creating dynamic, powerful web applications and websites.
PHP is a widely used, accessible, and efficient alternative to competitors such as Microsoft’s ASP. With this setup, you can easily run WordPress, Drupal, or Joomla CMS platforms.
Install Lighttpd HTTP Server
sudo apt install lighttpd
After installing Lighttpd, the commands below can be used to stop, start and enable Lighttpd service to always startup when the server boots up.
sudo systemctl stop lighttpd.service sudo systemctl start lighttpd.service sudo systemctl enable lighttpd.service
Install MariaDB Database Server
OrangeHRM also requires a database server. And MariaDB database server is a great place to start. To install it, run the commands below.
sudo apt install mariadb-server mariadb-client
After installing, the commands below can be used to stop, start and enable the MariaDB service always to start up when the server boots.
sudo systemctl stop mysql.service sudo systemctl start mysql.service sudo systemctl enable mysql.service
After that, run the commands below to secure the MariaDB server.
When prompted, answer the questions below by following the guide.
- Enter current password for root (enter for none): Just press the Enter
- Set root password? [Y/n]: Y
- New password: Enter password
- Re-enter new password: Repeat password
- Remove anonymous users? [Y/n]: Y
- Disallow root login remotely? [Y/n]: Y
- Remove test database and access to it? [Y/n]: Y
- Reload privilege tables now? [Y/n]: Y
Restart MariaDB server
sudo systemctl restart mysql.service
Install PHP FastCGI and Related Modules
PHP 7.1 isn’t available on Ubuntu default repositories… to install it, you will have to get it from third-party repositories.
Run the commands below to add the below third party repository to upgrade to PHP 7.1
sudo apt-get install software-properties-common sudo add-apt-repository ppa:ondrej/php
Then update and upgrade to PHP 7.1
sudo apt update
After that, run the commands below to install PHP7.1-FastCGI and related PHP modules. PHP-FastCGI is a version for Lighttpd web servers, while PHP is Apache2. Nginx is PHP-FPM.
Many PHP modules perform different functions. However, some important ones are always needed when developing PHP-based websites.
sudo apt install php7.1-cgi php7.1-mcrypt php7.1-cli php7.1-mysql php7.1-gd php7.1-imagick php7.1-recode php7.1-tidy php7.1-xmlrpc
The line above will allow PHP to function with many popular PHP-based websites and applications.
After installing PHP7.1-FastCGI, you can enable PHP-FastCGI modules by running the commands below.
sudo sudo lighttpd-enable-mod fastcgi sudo lighttpd-enable-mod fastcgi-php
If the commands above fail, install the package below.
sudo apt install libterm-readline-gnu-perl
Then run the commands to enable the modules again; this time, they should work.
sudo /etc/init.d/lighttpd force-reload
Configure Lighttpd PHP-FastCGI Settings
Now that Lighttpd and PHP0-FastCGI are installed, you may want to configure Lighttpd to use PHP server scripting properly. The default Lighttpd PHP-FastCGI configuration file is located at /etc/php/7.1/cgi/php.ini
Open the PHP Lighttpd configuration file by running the commands below
sudo nano /etc/php/7.1/cgi/php.ini
Then edit the file to suit your environment. Some crucial lines to consider:
file_uploads = On allow_url_fopen = On memory_limit = 256M upload_max_filesize = 100M
Next, open the Lighttpd site configuration file. By default, it’s stored at /etc/Lighttpd/lighttpd.conf
Use the main configuration file to set up advanced server global settings.
Lighttpd PHP-FastCGI configuration file is stored at /etc/lighttpd/conf-available/15-fastcgi-php.conf
Run the commands below to open the Lighttpd PHP-FastCGI default site configuration file
sudo nano /etc/lighttpd/conf-available/15-fastcgi-php.conf
Then confirm that PHP-FastCGI is configured, as shown in the highlighted portion below.
# -*- depends: fastcgi -*- # /usr/share/doc/lighttpd/fastcgi.txt.gz # http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi ## Start an FastCGI server for php (needs the php7.1-cgi package) fastcgi.server += ( ".php" => (( "bin-path" => "/usr/bin/php-cgi7.1", "socket" => "/var/run/lighttpd/php.socket", "max-procs" => 1, "bin-environment" => ( "PHP_FCGI_CHILDREN" => "4", "PHP_FCGI_MAX_REQUESTS" => "10000" ), "bin-copy-environment" => ( "PATH", "SHELL", "USER" ), "broken-scriptfilename" => "enable" )) )
Restart Lighttpd services
sudo systemctl restart lighttpd.service
Test PHP-CGI Setup
At this point, Lighttpd and PHP-FastCGI should be installed and ready. To test your Lighttpd PHP settings, create a blank file with the line below:
sudo nano /var/www/html/phpinfo.php
Then add the line in the file and save.
<?php phpinfo( ); ?>
Save the file and open your browser and browse to the server name or IP address followed by phpinfo.php
You should see something similar to the image below. If you do, then you’re all good!
Congratulations! You’ve successfully installed and configured Lighttpd and PHP on Ubuntu servers.
You may also like the post below: