Setup a LEMP Stack on Ubuntu 22.04

  • Linux
  • Nginx (pronounced “En-gin-ex”)
  • MySQL
  • PHP

Dependencies

$ sudo apt update
$ sudo apt install mysql-server-8.0 nginx php-fpm php-mysql git -y

Firewall Rules

$ sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
$ sudo apt install iptables-persistent -y
$ sudo systemctl enable netfilter-persistent
$ sudo iptables-save > /etc/iptables/rules.v4

MySQL Database

$ sudo mysql
ALTER USER 'root'@'localhost' 
IDENTIFIED WITH mysql_native_password by 'password';
$ sudo mysql_secure_installation
Securing the MySQL server deployment.Enter password for user root:
Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: Y
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.
$ mysql -u root -ppassword
mysql> CREATE USER 'appuser'@'%' IDENTIFIED BY 'apppassword';
mysql> create database mydb;
mysql> GRANT ALL PRIVILEGES ON mydb.* TO 'appuser'@'%';
mysql> FLUSH PRIVILEGES;
mysql> exit
$ mysql -u appuser -papppassword -e 'select version();'
+-------------------------+
| version() |
+-------------------------+
| 8.0.29-0ubuntu0.22.04.2 |
+-------------------------+

Nginx and PHP Config

server {
listen 80;
server_name _;
root /var/www/app;
index index.html index.htm index.php; location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
$ sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/app
$ sudo unlink /etc/nginx/sites-enabled/default
$ git clone https://github.com/ruanbekker/docker-php-nginx-mysql /tmp/git
$ sudo mv /tmp/git/app /var/www/app
$ mysql -u appuser -papppassword mydb <  /tmp/git/database/data.sql
$ mysql -u appuser -papppassword mydb -e 'select * from users limit 1;'
+----+-------+-----+---------+--------------+
| id | name | age | country | job |
+----+-------+-----+---------+--------------+
| 1 | tyler | 34 | tunisia | psychiatrist |
+----+-------+-----+---------+--------------+
try {
$host = getenv('MYSQL_HOST');
$db = getenv('MYSQL_DATABASE');
$user = getenv('MYSQL_USER');
$pass = getenv('MYSQL_PASSWORD');
$port = getenv('MYSQL_PORT');
$conn = new PDO(
"mysql:host=$host; dbname=$db", $user, $pass
);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);} catch(PDOException $e) {
echo "Connection failed: "
. $e->getMessage();
}
env[MYSQL_HOST] = 'localhost'
env[MYSQL_PORT] = '3306'
env[MYSQL_DATABASE] = 'mydb'
env[MYSQL_USER] = 'appuser'
env[MYSQL_PASSWORD] = 'apppassword'
$ sudo systemctl restart php8.1-fpm
$ sudo systemctl restart nginx

Test our PHP Application

Data returned from MySQL

Thank You

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store