Hack The Planet!

Spellbook > Linux

Spell #3 A PHP webserver on Debian

Assumptions

  • You have successfully cast spell #1

Open a root shell to the server

ssh debian-server
-i

Install the NGINX web server

apt update && apt -y install nginx

Install the MariaDB database server

apt -y install mariadb-server mariadb-client-compat

Secure the MariaDB installation

mysql_secure_installation

Optimize MariaDB for performance

cat << 'EOF' > /etc/mysql/mariadb.conf.d/99-performance-tuning.cnf
[mariadbd]

# Performance tuning for 4GB RAM

# Core memory tuning
innodb_buffer_pool_size = 1536M
innodb_log_buffer_size = 32M
max_connections = 50

# Temporary tables
tmp_table_size = 64M
max_heap_table_size = 64M

# Thread and table handling
thread_cache_size = 25
table_open_cache = 512
open_files_limit = 65535

# Query optimization
join_buffer_size = 2M
sort_buffer_size = 2M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
EOF
systemctl restart mariadb
systemctl status mariadb

Install PHP-FPM

apt -y install php-fpm

Install PHP Extensions

apt install -y php-bcmath php-curl php-gd php-imagick php-intl php-json php-mbstring php-mysql php-xml php-zip

Update NGINX configuration (optional)

Edit /etc/nginx/sites-available/default.

Add index.php to the line index index.html index.htm index.nginx-debian.html; so it becomes:

index index.html index.htm index.nginx-debian.html index.php;

Add the following under “pass PHP scripts to FastCGI server”:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php-fpm.sock;
  }

Restart NGINX and PHP-FPM

systemctl restart nginx
systemctl restart php8.4-fpm.service

Test PHP pages

Create a simple test file

mkdir -p /var/www/html/testphp
cat << 'EOF' > /var/www/html/testphp/index.php
<?php phpinfo(); ?>
EOF

Visit http://debian-server.domain.test/testphp/

Delete the test

rm -rf /var/www/html/testphp

Configure TLS using Let’s Encrypt

Install certbot

apt -y install certbot python3-certbot-nginx

Generate certificates

certbot certonly --dry-run --webroot -w /var/www/html -d domain.test,www.domain.test