Skip to content

Somik's Home

Server admins cheat codes

Menu
  • Bash
  • PHP
  • Arduino/ESP
  • About
    • eBook Reader
    • Codebin
    • User Management
    • Online Ruler
    • ShortURL Generator (Newer)
    • Video Downloader
    • ShortURL Generator (Older)
    • File Sharing
    • WhoIs Search
Menu

Ubuntu 20.04 Nginx+PHP

Posted on 2019-11-132022-01-10 by somik

This will install Nginx and PHP 7.x on Ubuntu 18.04 or later and perform basic configurations needed to get it up and running. This is used to create servers to serve static content as well as minor PHP scripts that need to run on it.

After we’ll set up SSL using LetsEncrypt and use it to transfer static and dynamic content over HTTPS.

Start by updating the server and installing Nginx as well as certbot:

sudo apt update
sudo apt upgrade
sudo apt install nginx nano python3-certbot-nginx

Then install PHP:

sudo apt install php-mysql php-gd php-imagick php-xmlrpc php-sqlite3 php-curl php-xml php-mbstring php-fpm

Edit PHP.ini file:

sudo nano /etc/php/7.*/fpm/php.ini

And add these at the bottom:

short_open_tag = On
display_errors = on
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
error_log = error_log
output_buffering = Off
date.timezone = "Asia/Singapore"
upload_max_filesize = 50M
post_max_size = 50M
memory_limit = 128M

Edit the nginx configuration file:

sudo nano /etc/nginx/nginx.conf 

And add this line to increase file upload size:

http {
	#...
        client_max_body_size 50M;
	#...
}

And enable PHP in Nginx site config files for your domain (where example.com is your domain)

sudo mkdir -p /var/www/example.com/public_html/
sudo nano /etc/nginx/sites-available/example.com.conf
sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf 

And edit it to make it look like this:

server {
        
        root /var/www/example.com/public_html/;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name example.com www.example.com;

        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;
        }


    listen 80;

}

Finally save and exit.

Now enable Nginx and PHP fpm to start on boot and run now.

sudo systemctl enable nginx php7.4-fpm
sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx

Now lets get a certificate for the domain from certbot:

sudo certbot --nginx --redirect -d example.com -d www.example.com

Follow the steps and you’ll get a certificate.

June 2022
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Jul    

Archives

  • July 2021
  • February 2021
  • September 2020
  • August 2020
  • July 2020
  • May 2020
  • March 2020
  • November 2019
  • October 2019

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2022 Somik's Home | Powered by Minimalist Blog WordPress Theme