Setting up the server

Useful#

Video:

Questions can be asked in the comments telegram: TG: fitgame

DNS setup#

Create a server, requirements:

We save the login and password, as well as the IP address, it will be in the article 45.131.40.167

Register a domain, in the article demo-fitgame.ru

Setting up an A record in the domain registrar panel

For demo-fitgame.ru (2nd level domain)

For subdomain game.demo-fitgame.ru (3nd level domain)

Changes usually take effect within 15 minutes, I recommend waiting

First steps#

Connecting to the server

ssh root@45.131.40.167

Getting a list of packages

apt update

We update them, answer questions install the package maintainer's version

apt upgrade

Installing and configuring a firewall

apt install ufw

We allow entry through ssh

ufw allow OpenSSH

Turn on the firewall

ufw enable

Create an admin user and give him rights

adduser admin
usermod -aG sudo admin

Reboot

reboot

Now log in as admin

ssh admin@45.131.40.167

Installing NGINX#

Installing NGINX

sudo apt install nginx

Allow for firewall

sudo ufw allow 'Nginx Full'

Installing Midnight Commander (file editor)

sudo apt install mc

Create a file with a minimal NGINX configuration

sudo mcedit /etc/nginx/sites-enabled/game

Content

server {
    listen 80;
    server_name demo-fitgame.ru;
}

Restart NGINX

sudo systemctl restart nginx

Installing certbot

sudo add-apt-repository universe
# sudo apt-get install software-properties-common # if it didn't work out
sudo apt install certbot python3-certbot-nginx

Launch certbot

sudo certbot --nginx --redirect

Checking automatic renewal

sudo certbot renew --dry-run

You can install only NGINX and skip PHP/nodeJS/SQL
If you skip, you need to run this command, otherwise you can skip it and go to the next one

sudo chmod -R 777 /var/www/

Installing PHP#

Installing PHP

sudo apt install php-fpm php-pgsql

Installing modul curl

sudo apt install php8.1-curl

Installing nodeJS#

Installing nodeJS и npm

sudo apt install nodejs
sudo apt install npm

Installing pm2 for manage

sudo npm install pm2 -g

Create a folder for nodeJS

sudo chmod -R 777 /var/www/
cd /var/www/
mkdir nodejs
cd nodejs/

Install the necessary npm for the project

npm install express
npm install socket.io

Create a file with a minimum project for nodeJS

mcedit game.js

Content

'use strict';

console.log('\x1b[36m%s', '-------------------------------');
console.log('\x1b[36m%s', 'Server init');

//Minimum debugging
process.env.NODE_ENV = 'production';

//Settings
let express = require('express'); //express
let app = express();
let http = require('http').Server(app);
let body_parser = require('body-parser');



app.disable('x-powered-by');


let server = http.listen(8000, () => {

});


app.use((req, res, next) => {
	body_parser.json()(req, res, err => {
		if (err) {
			return res.sendStatus(400); // Bad request
		}
		next();
	});
});
app.use(body_parser.urlencoded({ extended: true }));


app.use('/nodejs/:action', function (req, res) {
	console.log(req);
	res.send('OK');
});

Launch game.js

pm2 start /var/www/nodejs/game.js

We execute the command and do copy/past command output/span>

pm2 startup

Setup PostgreSQL#

Setup timezone

sudo timedatectl set-timezone Europe/Moscow

Setup Datebase

sudo apt-get install postgresql-14

Setup password for postgresql

sudo -u postgres psql postgres
\password postgres
\q

Edit in pg_hba.conf peer -> md5

sudo mcedit /etc/postgresql/14/main/pg_hba.conf

Restart Database

sudo service postgresql restart

Server Configuration and Testing#

Create an ftp user and give him rights

sudo adduser ftp

Upload files to the server, I recommend, via FileZilla:

Create folders

/var/www/php
/var/www/php/adminer/
/var/www/php/game/

Download adminer.

Unpack and rename to index.php. Upload to

/var/www/php/adminer/

Create index.php with text and upload it to

<?php phpinfo(); ?>
/var/www/php/game/

Create index.html with text and upload it to

Well done
/var/www/html/

Remove file index.nginx-debian.html

/var/www/html/index.nginx-debian.html

Final NGINX configuration

sudo mcedit /etc/nginx/sites-enabled/game
server {
    server_name demo-fitgame.ru;

    listen 443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/demo-fitgame.ru/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/demo-fitgame.ru/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;


    gzip on;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/javascript application/octet-stream application/x-javascript text/xml application/xml application/xml+rss text/javascript;


    root /var/www/html;


    #Panel for database
    location /sql/ {
        rewrite  ^/sql/(.*) /$1 break;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        root /var/www/php/adminer/;
    }

    #PHP
    location /php-game/ {
        rewrite  ^/php-game/(.*) /$1 break;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        root /var/www/php/game/;
    }

    #nodeJS
    location /nodejs-game {
        proxy_pass http://localhost:8000/nodejs;
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

}

server {
    if ($host = demo-fitgame.ru) {
        return 301 https://$host$request_uri;
    }


    listen 80;
    server_name demo-fitgame.ru;
}

Restart NGINX

sudo systemctl restart nginx

Checking the server: