NGINX
jack
, you can use any other username.NGINX & firewall
sudo apt update
sudo apt install -y nginx
Configure firewall
sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
sudo ufw allow 'Nginx Full'
default NGINX
Virtual Host
sudo usermod -a -G www-data $USER
NGINX is setup but now, we have to setup Virtual hosts (or VHost). We take a basic example with just PHP 8.2 and an index.php
, but it's same concept for any PHP app like Laravel or Symfony.
Create NGINX conf for this app
sudo vim /etc/nginx/sites-available/my-domain.localhost
Insert these infos and save file
server {
listen 80;
root /var/www/my-domain;
index index.php index.html index.htm index.nginx-debian.html;
server_name my-domain.localhost;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Setup rights on web server folder /var/www
, create root folder for the app and index.php
sudo chown -R $USER:www-data /var/www
sudo mkdir /var/www/my-domain
sudo touch /var/www/my-domain/index.php
sudo vim /var/www/my-domain/index.php
Fill index.php
with basic infos
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Domain</title>
</head>
<body>
<div style="text-align: center; font-weight: bold; font-family: sans-serif; margin: 5rem 0">
This is my new domain!
<br/>
<?php
echo $_SERVER['SERVER_SOFTWARE'];
?>
</div>
</body>
</html>
Make symbolic link with NGINX conf and sites-enabled
to enable this conf, check NGINX configuration and reload NGINX to refresh web server
sudo ln -s /etc/nginx/sites-available/my-domain.localhost /etc/nginx/sites-enabled
sudo nginx -t
sudo service nginx reload
And now, you can access to you domain to http://my-domain.localhost
Next step: install and configure phpMyAdmin
Allow big files uploading
NGINX default conf allow 2 Mo files max in upload, if you want to change it follow these steps
NGINX
sudo vim /etc/nginx/nginx.conf
http {
# ...
client_max_body_size 500M;
}
Check PHP version used by your application,PHP
php -v
give you just PHP CLI version, if don't setup VHost, it's PHP version of your app but if you have a VHost you have to check PHP version of conf. And update current php.ini
For example if current version is 8.2 (found with php --ini
)sudo vim /etc/php/8.2/fpm/php.ini
post_max_size = 500M
# ...
upload_max_filesize = 500M
Restart NGINX and PHP
sudo service nginx restart
sudo service php8.2-fpm restart
You can check with phpinfo()
.
PHP version and NGINX
When you execute php
command into your terminal, you have a current PHP version than you can check it with php -v
. But yours VHosts can always use old version of PHP, you will have to update NGINX conf for each VHost if you want to change their version of PHP (it's optional). You can keep VHost with old version of PHP, if you let this version on your system, it's useful to update version if you use modern syntax in your PHP app, for example type of parameters is available with latests PHP versions.
If you want to update a VHost NGINX conf, check it in /etc/nginx/sites-available/
and edit it, like:
sudo vim /etc/nginx/sites-available/my-domain
You will have some infos but search this line
server {
# ...
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
#...
}
In this example, this VHost use PHP 7.2 version but if you installed PHP 8.0, you can update it
server {
# ...
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
}
#...
}
And your app will use new PHP version.
Troubles
List virtual hosts with Nginx
grep server_name /etc/nginx/sites-enabled/* -RiI
Uninstall NGINX
sudo apt-get purge nginx nginx-common
sudo apt-get autoremove
VHost works on Chrome but not on Firefox
open Preferences -> Advanced -> Network open Settings and check manual proxy config and set proxy HTTP 127.0.0.1 and port 80