scripts lineage2  :::  Server Packs lineage 2 — best scripts for admin servers   :::  Projects lineage Turnkey
l2-scripts.com  Freelance Studio

ArticlesTechnical guides9 min read

Installing Apache or Nginx, PHP 8 and MariaDB on Ubuntu 22.04/24.04

How to set up a web server for an L2 project website on Ubuntu: Apache or Nginx, PHP 8 via PHP-FPM, MariaDB, a firewall, HTTPS and correct file permissions.

The php5 and libapache2-mod-auth-mysql packages, the gksudo editor and service control through /etc/init.d from old guides no longer exist in current Ubuntu releases. Below is an up-to-date setup of a web server for a project website (home page, registration, user panel, statistics) on Ubuntu 22.04 or 24.04 LTS. All commands are run as a regular user with sudo rights.

What gets installed

UbuntuPHP from the repositoryPHP-FPM serviceMariaDB
22.04 LTS8.1php8.1-fpm10.6
24.04 LTS8.3php8.3-fpm10.11

The examples use PHP 8.3 (Ubuntu 24.04); on 22.04 replace 8.3 with 8.1. php -v shows the installed version, and ls /run/php/ shows the name of the PHP-FPM socket.

Apache or Nginx? Apache understands .htaccess files, which most CMSs and L2 website scripts rely on. Nginx serves static files faster and uses less memory, but .htaccess rules have to be rewritten into the site config. Pick one: the two cannot both listen on port 80.

Step 1. Update the system

sudo apt update
sudo apt upgrade -y

Step 2. MariaDB

sudo apt install -y mariadb-server mariadb-client
sudo systemctl enable --now mariadb
sudo mysql_secure_installation

The security script asks a few questions. Answer Y to removing anonymous users, disallowing remote root login and removing the test database. On Ubuntu root logs in to MariaDB with sudo mysql (unix_socket authentication), so you do not have to switch it or set a root password. In newer versions the same tool is called mariadb-secure-installation.

Create a database and a separate user for the website:

sudo mysql

CREATE DATABASE l2site CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'l2site'@'localhost' IDENTIFIED BY 'CHANGE_ME_StrongPass';
GRANT ALL PRIVILEGES ON l2site.* TO 'l2site'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Instead of MariaDB you can install MySQL 8.0 (sudo apt install mysql-server); the commands are the same and the service is called mysql.

Website access to the game server databaseTo show online counts and rankings and to register accounts, the website needs access to the game server database. If it is on another machine, create a user there that is limited to the web server IP and has minimal rights (SELECT, plus INSERT on the accounts table for registration), and open port 3306 only to that IP. Never open the database port to everyone.

Step 3, option A. Apache and PHP-FPM

sudo apt install -y apache2 php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl
sudo a2enmod proxy_fcgi setenvif rewrite headers
sudo a2enconf php8.3-fpm
sudo systemctl restart apache2

The proxy_fcgi module hands PHP scripts to PHP-FPM, and rewrite is needed for friendly URLs and .htaccess rules. Do not install libapache2-mod-php alongside: it is the old way of running PHP and switches Apache to the less efficient mpm_prefork mode.

Create a virtual host in /etc/apache2/sites-available/example.com.conf (replace example.com with your domain). nano is a convenient editor: sudo nano /etc/apache2/sites-available/example.com.conf.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

Create the site folder, enable the host, disable the default one and test the config:

sudo mkdir -p /var/www/example.com/public_html
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

Step 3, option B. Nginx and PHP-FPM

sudo apt install -y nginx php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl
sudo mkdir -p /var/www/example.com/public_html

The site config goes into /etc/nginx/sites-available/example.com:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/example.com/public_html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

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

    location ~ /\.(ht|git) {
        deny all;
    }
}

Enable the site, disable the default one and test the config:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
Nginx does not read .htaccessMove rewrite rules and access restrictions from .htaccess into the server block. Block access to the script's internal folders (configs, logs, template cache) with deny all, otherwise they can be downloaded by a direct link.

Step 4. Configure PHP

PHP settings for websites live in /etc/php/8.3/fpm/php.ini (console scripts use a separate /etc/php/8.3/cli/php.ini). Typical values:

memory_limit = 256M
upload_max_filesize = 32M
post_max_size = 32M
max_execution_time = 60
date.timezone = UTC
expose_php = Off

Restart PHP-FPM after changes: sudo systemctl restart php8.3-fpm. php -m lists the loaded extensions; missing ones are installed as php-name packages, for example php-bcmath or php-redis.

To check that PHP works, create a test file:

echo '<?php phpinfo();' | sudo tee /var/www/example.com/public_html/info.php

Open http://example.com/info.php in a browser: the Server API line should say FPM/FastCGI. Be sure to delete the file afterwards: sudo rm /var/www/example.com/public_html/info.php.

Step 5. ufw firewall

sudo ufw allow OpenSSH
sudo ufw allow "Apache Full"      # for Nginx: sudo ufw allow "Nginx Full"
sudo ufw enable
sudo ufw status

The Apache Full and Nginx Full profiles open ports 80 and 443. Add the SSH rule before enabling ufw, or you will lock yourself out; if SSH runs on a non-standard port, open that port instead, for example sudo ufw allow 2222/tcp. If the game server runs on the same machine, open its ports too: sudo ufw allow 2106/tcp and sudo ufw allow 7777/tcp. There is no need to open the database port 3306.

Step 6. HTTPS certificate

A free Let's Encrypt certificate is issued with the certbot tool. Before that, the A records for the domain and www must point to the server IP, and port 80 must be open.

# Apache
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com

# Nginx
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

# test automatic renewal
sudo certbot renew --dry-run

Certbot adds the HTTPS settings and an http-to-https redirect to the site config by itself. Renewal runs automatically from a systemd timer.

Step 7. Where the site files live and permissions

The site files go into /var/www/example.com/public_html. The web server and PHP-FPM run as the www-data user. A convenient scheme: the files are owned by your user (the one you upload the site with over SFTP) with the www-data group; folders 755, files 644, and write access only to folders where the script stores data (cache, uploads, compiled templates).

sudo chown -R $USER:www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 755 {} \;
sudo find /var/www/example.com -type f -exec chmod 644 {} \;
# write access only where the site stores data (example: cache)
sudo chmod -R g+w /var/www/example.com/public_html/cache
# the file with the database password (name depends on the site script)
sudo chmod 640 /var/www/example.com/public_html/config.php
Never use 777777 permissions let any process on the server modify the site files and are a common cause of hacks. If a script reports Permission denied, give the www-data group write access to that one folder only. Do not leave test files like info.php, an open phpMyAdmin or backup archives in the site folder.

Managing services and useful paths

CommandWhat it does
sudo systemctl status apache2service status (same for nginx, php8.3-fpm, mariadb)
sudo systemctl restart namerestart a service
sudo systemctl reload apache2reload the config without dropping connections
sudo systemctl enable nameenable start at boot
sudo apache2ctl configtest, sudo nginx -ttest the config before a restart
sudo journalctl -u php8.3-fpmservice log
PathContents
/var/www/website files
/etc/apache2/sites-available/Apache virtual hosts
/etc/nginx/sites-available/Nginx site configs
/etc/php/8.3/fpm/php.iniPHP settings for websites
/etc/php/8.3/fpm/pool.d/www.confPHP-FPM pool: user, number of processes
/etc/mysql/mariadb.conf.d/50-server.cnfMariaDB server settings
/var/log/apache2/, /var/log/nginx/web server logs
Hosting control panelIf the server is managed by a control panel (a web interface for sites, mail and databases), create sites, databases and certificates through it and do not edit these configs by hand: the panel will overwrite your changes.

Setting up the game server on Windows is covered in the Windows setup guide. We can also deploy the website and the server for you as a turnkey project; ready-made server packs are available too.

Published 22 November 2026

Read also

Need help with your server?

We help with the server pack, setup, protection and launch — one-off or on an ongoing basis.

L2‑Scripts newsletter

Get L2‑Scripts news by email

And 5% off all studio services while you stay subscribed.

The discount is valid while you are subscribed: when ordering, tell the manager your subscription address on Telegram.