Connenting to server with ssh in terminal:
ssh root@ip -p22
Update the server:
apt update
apt upgrade
Configure SSH:
Change the #Port 22 to other thing
nano /etc/ssh/sshd_config
Change password of ssh:
Enter code and type new password in terminal for root user
passwd
Restart the ssh:
systemctl reload sshd
Connect the server with new pass!
Allow new port on ufw:
ufw allow [Newport]/tcp
Installing necessary deps:
node:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
apt install nodejs
npm:
apt install npm
pm2:
npm i -g pm2
Mysql + phpMyAdmin:
Installing
apt install mysql-server
apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl php-fpm php-mysql
apt install libapache2-mod-php
a2enmod php7.4
systemctl restart apache2
Configurations
Apache2
nano /etc/apache2/apache2.conf
Add this to last line and save it: Include /etc/phpmyadmin/apache.conf
Mysql
mysql -uroot -p
In mysql:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'TheStrongPassword';
CREATE USER 'TheNewUser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'TheStrongPassword';
GRANT ALL PRIVILEGES ON * . * TO 'TheNewUser'@'localhost';
FLUSH PRIVILEGES;
Run this to secure mysql
mysql_secure_installation
Create shortcut phpmyadmin folder to nginx folder
ln -s /usr/share/phpmyadmin/var/www/yourDomain.com/phpMyAdmin
phpmyadmin security options
cd /var/www/yourDomain.com/
mv phpmyadmin StrongPassword
Edit config file to secure phpMyAdmin
This should be set to a random string of at least 32 chars nano /etc/phpmyadmin/conf.d/pma_secure.php
<?php $cfg['blowfish_secret'] = 'random32Chars'; $i=0; $i++; $cfg['Servers'][$i]['auth_type'] = 'cookie'; $cfg['Servers'][$i]['AllowNoPassword'] = false; $cfg['Servers'][$i]['AllowRoot'] = false; ?>
(Optional security function)
For new other server creating authentication gateway in terminal enter and confirm the 8 char pass to get codded pass openssl passwd
Enter the username and codded pass nano /etc/nginx/pma_pass newUser:strongPass
Nginx:
First we remove apache2
systemctl stop apache2
apt-get purge apache2 apache2-utils apache2-bin apache2.2-common
apt-get autoremove
Installing nginx
apt install nginx
ufw allow 'Nginx Full'
rm /etc/nginx/sites-enabled/default
Make main domain
mkdir /var/www/yourDomain.com
chown -R $USER:$USER /var/www/yourDomain.com
Installing ssl
nano /etc/ssl/cert.pem Paste ssl certification >>IN ORIGIN SECTION<<
nano /etc/ssl/key.pem Paste ssl key >>IN ORIGIN SECTION<<
Edit nginx main server
nano /etc/nginx/sites-available/yourDomain.com
server { listen 80; listen [::]:80; server_name yourDomain.com www.yourDomain.com; root /var/www/qlotter.com; index index.php index.html index.htm index.nginx-debian.html; return 302 https://$server_name$request_uri; } server { # SSL configuration listen 443 ssl http2; listen [::]:443 ssl http2; ssl_certificate /etc/ssl/cert.pem; ssl_certificate_key /etc/ssl/key.pem; server_name yourDomain.com www.yourDomain.com; root /var/www/yourDomain.com; index index.php index.html index.htm index.nginx-debian.html; location / { proxy_set_header X-FORWARD-FOR $remote_addr; proxy_set_header Host $http_host; proxy_pass http://localhost:3000; } # next 12 upgrade location /_next/webpack-hmr { proxy_pass http://localhost:3000/_next/webpack-hmr; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location ~ /\.ht { deny all; } }
Unlink and reload server
ln -s /etc/nginx/sites-available/yourDomain.com /etc/nginx/sites-enabled/
unlink /etc/nginx/sites-enabled/default
nginx -t
systemctl reload nginx
Git
git config --global user.name "yourUserName"
git config --global user.email "yourEmail"
Get repository from Git
git clone yourRepoAddress
Deploy and save in pm2
Go to your repo folder and do these steps: First you have to delete packaje-lock.json file. We use pm2 because if we close the ssh, app will run automatically
npm i
npx prisma migrate deploy // If you use prisma
npm run build
pm2 start npm --name=nextjs -- start
pm2 startup
pm2 save
Dario L. Mindoro
Author of Mindworksoft.com, Full-stack developer, interested in media streaming, automation, photography, AI, and digital electronics.