PHP en MySQL/MariaDB installeren

(Maak je server klaar voor dynamische websites zoals WordPress en Laravel)

Een standaard webserver kan alleen statische HTML-bestanden serveren. Voor dynamische websites heb je een programmeertaal (PHP) en een database (MySQL/MariaDB) nodig. Dit heet een LAMP (Linux, Apache, MySQL, PHP) of LEMP (Linux, Nginx, MySQL, PHP) stack.

1. PHP installeren

  • Installeer PHP-FPM en extensies:
    sudo apt update
    sudo apt install php8.1-fpm php8.1-mysql php8.1-mbstring php8.1-xml php8.1-curl php8.1-zip php8.1-gd

  • Configuratie voor Nginx:
    Open /etc/nginx/sites-available/default en voeg toe binnen je server { } block:

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

    Herlaad Nginx:
    sudo systemctl reload nginx

  • Configuratie voor Apache:
    sudo a2enmod proxy_fcgi setenvif
    sudo a2enconf php8.1-fpm
    sudo systemctl restart apache2


2. MariaDB installeren (Aanbevolen database)

  • Installeer MariaDB:
    sudo apt install mariadb-server

  • Beveilig de installatie:
    sudo mysql_secure_installation

    • Druk op Enter bij root-wachtwoord

    • Stel een sterk wachtwoord in

    • Verwijder anonieme gebruikers

    • Blokkeer root login op afstand

    • Verwijder testdatabase

    • Herlaad rechten


3. Een database en gebruiker aanmaken

  • Log in op MariaDB:
    sudo mysql -u root -p

  • Maak een database aan:
    CREATE DATABASE mijnwebsite_db;

  • Maak een gebruiker aan:
    CREATE USER 'mijnwebsite_user'@'localhost' IDENTIFIED BY 'een_sterk_wachtwoord';

  • Geef rechten:
    GRANT ALL PRIVILEGES ON mijnwebsite_db.* TO 'mijnwebsite_user'@'localhost';

  • Herlaad rechten en sluit af:
    FLUSH PRIVILEGES;
    EXIT;


Samenvatting

  • Installeer PHP-FPM en configureer voor Nginx of Apache

  • Gebruik MariaDB als veilige, snelle database

  • Maak een aparte databasegebruiker voor je webapplicatie


Tip: Test PHP met een info.php bestand in /var/www/html met de inhoud <?php phpinfo(); ?>. Verwijder dit bestand daarna direct.

Hulp nodig? Krijg je een 502 Bad Gateway (Nginx) of 503 Service Unavailable (Apache)? Controleer de status van PHP-FPM met:
systemctl status php8.1-fpm

Was dit antwoord nuttig? 0 gebruikers vonden dit artikel nuttig (0 Stemmen)

Powered by WHMCompleteSolution