Blog / Others/ Installing, Configuring, and Using Lighttpd on CentOS 7 64-bit

Installing, Configuring, and Using Lighttpd on CentOS 7 64-bit

CentOS 7 64bit安装lighttpd服务器、配置及使用

Initial Server Configuration

Before installing Lighttpd, it is recommended to perform some basic server configuration to ensure system stability, security, and usability.

1. Set System Timezone and Synchronize Time

Use NTP service to synchronize accurate time and write it to the hardware clock to prevent loss after reboot.

timedatectl set-timezone Asia/Shanghai
yum install -y ntp
systemctl start ntpd
systemctl enable ntpd
hwclock --systohc

2. Install Chinese Fonts (Optional)

If the server needs to handle or display Chinese filenames (e.g., as a file server), you can install Chinese font packages.

yum install -y fontconfig
# Install common Chinese fonts, for example:
yum install -y wqy-microhei-fonts

3. Modify SSH Port

Important: Before changing the port, ensure the new port is allowed in the firewall; otherwise, SSH connections may be lost.

  1. Edit SSH config: vi /etc/ssh/sshd_config, find line #Port 22, change to Port new_port (e.g., 2222).
  2. Add port to firewall: firewall-cmd --permanent --add-port=new_port/tcp
  3. Reload firewall: firewall-cmd --reload
  4. Restart SSH: systemctl restart sshd
  5. Verify status: systemctl status sshd

4. Enable BBR Congestion Control

BBR can improve network throughput. Use this script to install and enable it:

wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh && chmod +x bbr.sh && ./bbr.sh

After installation, reboot the server. Then verify BBR is active:

sysctl net.ipv4.tcp_congestion_control
# Should return: net.ipv4.tcp_congestion_control = bbr
lsmod | grep bbr
# Should show tcp_bbr module (may not appear on some VPS, which is normal)

5. Disable SELinux

SELinux may interfere with service operation; you can disable it for testing or if not needed.

  • Temporary disable: setenforce 0
  • Permanent disable: Edit /etc/selinux/config, change SELINUX=enforcing to SELINUX=disabled, then reboot.

Install Lighttpd via Package Manager

CentOS 7 default repositories do not include Lighttpd; first install the EPEL repository.

yum install -y epel-release
yum update -y
yum install -y lighttpd

After installation, check the installed version:

lighttpd -v
# or
rpm -qa | grep lighttpd

Compile and Install Lighttpd

For specific versions or custom features, compile from source. Example for version 1.4.48:

# 1. Install build dependencies
cd /tmp
yum install -y gcc gcc-c++ autoconf automake make pcre-devel zlib-devel openssl-devel bzip2

# 2. Download and extract source
wget https://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.48.tar.gz
tar -zxvf lighttpd-1.4.48.tar.gz
cd lighttpd-1.4.48

# 3. Configure, compile, and install
./configure
make
make install

Note: Compiled installation does not create a systemd service file by default; manual configuration is required. Use package manager installation unless necessary.

Lighttpd Configuration File Structure

After package installation, main configuration paths:

  • Main config: /etc/lighttpd/lighttpd.conf
  • Modules config: /etc/lighttpd/modules.conf
  • Modules directory: /etc/lighttpd/conf.d/
  • Virtual hosts directory: /etc/lighttpd/vhosts.d/

Lighttpd Service Management Commands

CentOS 7 uses systemd for service management.

  • Start: systemctl start lighttpd
  • Stop: systemctl stop lighttpd
  • Restart: systemctl restart lighttpd
  • Status: systemctl status lighttpd
  • Enable at boot: systemctl enable lighttpd
  • Disable at boot: systemctl disable lighttpd

Uninstall Lighttpd:

# For yum installation
yum remove lighttpd

Firewall Configuration

CentOS 7 uses firewalld by default. Allow web service ports (default 80 or 443).

# Allow HTTP (80)
firewall-cmd --permanent --add-service=http
# Allow HTTPS (443)
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
firewall-cmd --list-all

To switch to traditional iptables:

systemctl stop firewalld
systemctl disable firewalld
yum install -y iptables-services
systemctl start iptables
systemctl enable iptables

Then edit /etc/sysconfig/iptables to configure rules.

Install and Configure PHP for Lighttpd

Lighttpd works with PHP-FPM via FastCGI.

  1. Install PHP and components:
    yum install -y php php-cli php-fpm php-mysqlnd php-gd php-mbstring
  2. Configure PHP-FPM: Edit /etc/php-fpm.d/www.conf, set user and group to lighttpd (or match Lighttpd user).
    user = lighttpd
    group = lighttpd
    listen = 127.0.0.1:9000
  3. Enable FastCGI in Lighttpd: Ensure /etc/lighttpd/modules.conf includes include "conf.d/fastcgi.conf".
  4. Configure FastCGI: Edit /etc/lighttpd/conf.d/fastcgi.conf, add PHP handling:
    fastcgi.server += ( ".php" =>
        ((
            "host" => "127.0.0.1",
            "port" => "9000",
            "broken-scriptfilename" => "enable"
        ))
    )
  5. Start and enable services:
    systemctl start php-fpm
    systemctl enable php-fpm
    systemctl restart lighttpd
  6. Test PHP: Create info.php in web root (e.g., /var/www/html) with content <?php phpinfo(); ?>. Access via browser to confirm PHP info page.

Install FTP Service (Optional)

For FTP, consider the lightweight and secure Pure-FTPd.

yum install -y pure-ftpd
systemctl start pure-ftpd
systemctl enable pure-ftpd
# Allow FTP in firewall
firewall-cmd --permanent --add-service=ftp
firewall-cmd --reload

Pure-FTPd configuration is straightforward; refer to official docs for user and permission setup.

Post a Comment

Your email will not be published. Required fields are marked with *.