知識庫

Installing a web server stack (LEMP and LAMP) 列印

  • lemp, lamp, nginx, apache, php
  • 0

Overview

LEMP and LAMP are the classic web hosting stacks: Linux plus Nginx (the "E") or Apache, MySQL/MariaDB and PHP. This article installs a working stack on a Debian/Ubuntu Pod so you can host websites and applications.

Before you begin

  • A Pod running Ubuntu or Debian with sudo access.
  • A DNS record pointing your domain at the Pod's IP (for TLS later).

LEMP (Nginx + MariaDB + PHP-FPM)

  1. Install the components:

bash sudo apt update sudo apt install -y nginx mariadb-server php-fpm php-mysql

  1. Secure the database:

bash sudo mysql_secure_installation

  1. Enable PHP in Nginx. Create /etc/nginx/sites-available/example.com:

```nginx server { listen 80; server_name example.com www.example.com; root /var/www/example.com/public; index index.php index.html;

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

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

} ```

  1. Enable the site and reload:

bash sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx

LAMP (Apache + MariaDB + PHP)

bash sudo apt install -y apache2 mariadb-server php libapache2-mod-php php-mysql sudo a2enmod rewrite sudo systemctl restart apache2

Apache serves from /var/www/html by default; create a VirtualHost in /etc/apache2/sites-available/ for each site and enable it with a2ensite.

Open the firewall

bash sudo ufw allow 'Nginx Full' # or 'Apache Full'

Add HTTPS (recommended)

bash sudo apt install -y certbot python3-certbot-nginx # or python3-certbot-apache sudo certbot --nginx -d example.com -d www.example.com

Verify

bash curl -I http://example.com

Troubleshooting

  • Problem: PHP downloads instead of executing. Solution: PHP-FPM is not wired into the server config; check the fastcgi_pass socket path with ls /run/php/.
  • Problem: 403 Forbidden. Solution: check file ownership and that the document root exists with an index file.
  • Problem: MariaDB access denied. Solution: use sudo mysql for admin tasks and create app users with CREATE USER ... IDENTIFIED BY ....

Related articles

  • [Updating the operating system]
  • [Installing Ubuntu Server]
  • [Initial server hardening after installation]

這篇文章有幫助嗎?

« 返回