Tabla de Contenidos

LEMP stack

Let's set up a LEMP stack (Linux, Nginx, MySQL, PHP) on your Ubuntu server. This combination allows you to serve dynamic web pages and applications written in PHP. Here are the steps:

Install

sudo apt update
sudo apt install nginx nginx-extras
sudo ufw allow 'Nginx HTTP'
sudo apt install mysql-server
sudo mysql_secure_installation # - Secure your MySQL installation:
sudo apt install php php-fpm php-mysql

Configure Nginx to Use PHP:

sudo nano /etc/nginx/sites-available/example.com
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Test PHP with Nginx:

sudo nano /var/www/example.com/html/info.php
<?php phpinfo(); ?>

Create a MySQL database and user:

sudo mysql
CREATE DATABASE mydb;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Troubleshooting & tips