Skip to content

Instantly share code, notes, and snippets.

@lukewatts
Last active June 9, 2022 00:16
Show Gist options
  • Save lukewatts/3353aed17ee2ce481a53448fd4df3f68 to your computer and use it in GitHub Desktop.
Save lukewatts/3353aed17ee2ce481a53448fd4df3f68 to your computer and use it in GitHub Desktop.
Nginx & PHP 8.1 FPM - Ubuntu Installation
# ----------------------------------------
# Install Nginx
# ----------------------------------------
## Update and Upgrade packages
sudo apt update
sudo apt upgrade
## Install Nginx
sudo apt install nginx
## Add Nginx to firewall
sudo ufw allow 'Nginx HTTP'
# ----------------------------------------
# Install PHP
# ----------------------------------------
## Add PHP PPA
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
# Install PHP FPM for Nginx
sudo apt install php8.1-fpm
# Install common extension for PHP
sudo apt install php8.1-common php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-redis php8.1-intl -y
# Configure PHP
sudo nano /etc/php/8.1/fpm/php.ini
# Set the following in /etc/php/8.1/fpm/php.ini (remove # before)
# upload_max_filesize = 32M
# post_max_size = 48M
# memory_limit = 256M
# max_execution_time = 600
# max_input_vars = 3000
# max_input_time = 1000
# ----------------------------------------
# Configure Nginx
# ----------------------------------------
# Create root web directory for your domain (where <you_domain> = your-domain.tld)
sudo mkdir /var/www/<your_domain>
# Make current user owner of this folder
sudo chown -R $USER:$USER /var/www/<your_domain>
# Open config for your domain
sudo nano /etc/nginx/sites-available/<your_domain>
# Add the following...
# server {
# listen 80;
# server_name your_domain www.your_domain;
# root /var/www/your_domain;
#
# 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;
# }
# }
# Activate the configuration
sudo ln -s /etc/nginx/sites-available/<your_domain> /etc/nginx/sites-enabled/
# Unlink the default
sudo unlink /etc/nginx/sites-enabled/default
# To go back to default config...
# sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
# Test your configuration
sudo nginx -t # You shouold see 'test is successful!' at the end of the output
# Reboot Nginx
sudo systemctl reload nginx
# ----------------------------------------
# Setup SSL with Lets Encrypt
# ----------------------------------------
# Edit domain config
sudo nano /etc/nginx/sites-available/<your_domain>
# ...and add/overwrite this line
# server_name <your_domain> www.<your_domain>;
# Check nginx configuration is valid
sudo nginx -t
# Restart nginx service
sudo systemctl reload nginx
# Allow SSl only
sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'
## Set nginx configuration for LetsEncrypt
## NOTE: This will add a second server {} block, that is normal and valid
sudo certbot --nginx -d luke-watts.com -d www.luke-watts.com
# ----------------------------------------
# Setup permission for SFTP (e.g. FileZilla)
# ----------------------------------------
sudo usermod -a -G www-data $USER
sudo chgrp -R www-data /var/www/<your_domain>/html
sudo chmod -R g+w /var/www/<your_domain>/html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment