Skip to content

Nginx, PHP and MySQL

Installation

sh
sudo apt update
sudo apt install nginx
sudo apy install php-fpm php-mysql php-cli
  • php-fpm: FastCGI Process manager to run PHP with Nginx
  • php-mysql: Extention to connect PHP to MySQL
  • php-fpm: To run PHP on the terminal, useful for testing

Nginx

path: /etc/nginx/sites-available/default

sh
# Default Server configuration
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
server {
	listen 80;
	listen [::]:80;

	server_name example.com;

	root /var/www/example.com;
	index index.html;

	location / {
		try_files $uri $uri/ =404;
	}
}

Nginx PHP configuration

sh
# Server PHP configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;
	#root /home/Projects/html;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

	# pass PHP scripts to FastCGI server
	#
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
	
		# With php-fpm (or other unix sockets):
		fastcgi_pass unix:/run/php/php8.3-fpm.sock;

		# With php-cgi (or other tcp sockets):
		#fastcgi_pass 127.0.0.1:9000;

		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny all;
	}
}

Nginx Proxy, HTTPS & Socket.io

sh
server {
    # Redireciona HTTP para HTTPS
    listen 80;
    server_name a.webknet.app www.a.webknet.app;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
#    server_name a.webknet.app www.a.webknet.app;

    # Caminhos dos certificados SSL (ajuste conforme sua instalação do Certbot)
    ssl_certificate /etc/ssl/private/cert.pem;
    ssl_certificate_key /etc/ssl/private/key.pem;

    # Configurações recomendadas de SSL
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;

    # Configuração do proxy
    location / {
        proxy_pass http://localhost:3000; # Endereço do seu backend
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    location /socket.io/ { 
        proxy_pass http://localhost:3000; 
        proxy_http_version 1.1; 
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection "upgrade"; 
        proxy_set_header Host $host; 
    } 
}

How To Setup Nginx For Multi-Sites

Configure Nginx to support multiple-sites (virtual hosts) on the same server
Basic Structure

  • First, create the directories for each site:

bash

sh
sudo mkdir -p /var/www/site1.com/html
sudo mkdir -p /var/www/site2.com/html

Adjuts Permissions

  • Set appropriate permissions:

bash

sh
sudo chown -R $USER:$USER /var/www/site1.com/html
sudo chown -R $USER:$USER /var/www/site2.com/html
sudo chmod -R 755 /var/www

Create Separate Configuration Files

  • Nginx uses per-site configuration files, typically in /etc/nginx/sites-available
  • Create one for each site

For site1.com
bash

sh
sudo nano /etc/nginx/sites-available/site1.com
sh
#Adicione esta configuração:

server {
	listen 80;
	listen [::]:80;

	server_name site1.com www.site1.com;

	root /var/www/site1.com/html;
	index index.html index.htm;

	location / {
		try_files $uri $uri/ =404;
	}
}

For site2.com
bash

sh
sudo nano /etc/nginx/sites-available/site2.com
sh
#Adicione esta configuração:

server {
	listen 80;
	listen [::]:80;

	server_name site2.com www.site2.com;

	root /var/www/site2.com/html;
	index index.html index.htm;

	location / {
		try_files $uri $uri/ =404;
	}
}

Enable Sites

  • Create symbolic links to activate the sites:

bash

sh
sudo ln -s /etc/nginx/sites-available/site1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site2.com /etc/nginx/sites-enabled/

Test Configuration

  • Check for errors in syntax:

bash

sh
sudo nginx -t

Restart Nginx

  • Apply changes:

bash

sh
sudo systemctl restart nginx

Web Folder Permissions for NGINX user www-data

sh
chown -R www-data:www-data /caminho/customizado/seu-site 
chmod -R 755 /caminho/customizado/seu-site

MySQL

MySQL Docker Image

sh
docker pull mysql

Run Docker MySQL Image

sh
docker run --name mysql -p 3306:3306 -v mysql_volume:/var/lib/mysql/ -d -e "MYSQL_ROOT_PASSWORD=1234" mysql