Skip to content

Instantly share code, notes, and snippets.

@Harsh-js
Last active January 5, 2024 11:47
Show Gist options
  • Select an option

  • Save Harsh-js/615680166703a2d2d082b3e0838041dd to your computer and use it in GitHub Desktop.

Select an option

Save Harsh-js/615680166703a2d2d082b3e0838041dd to your computer and use it in GitHub Desktop.
Ubuntu Server Setup

EC2 Server setup - nodejs, nginx, nextjs, php, mysql, mongodb, redis

Switch to root user

sudo su

Update Packages

sudo apt-get update

Install Nginx

sudo apt install nginx -y
# verify installation
sudo nginx -v

Setup SSL

sudo snap install --classic certbot

sudo ln -s /snap/bin/certbot /usr/bin/certbot

sudo nano /etc/nginx/sites-available/your_domain

...
server_name your_domain www.your_domain;
...

sudo nginx -t

sudo systemctl reload nginx

sudo ufw status

sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'

sudo ufw status

sudo certbot --nginx -d your_domain -d your_domain

sudo certbot renew --dry-run

Setup Firewall (ufw)

# if ufw is not installed
sudo apt-get install ufw

sudo ufw enable
sudo ufw allow 'Nginx FULL'
sudo ufw allow 'OpenSSH'
sudo ufw allow 22

Now visit http://localhost:80 and you would be able to see default nginx welcome page.

Configure php

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

sudo apt install php7.4-{fpm,cli,common,gd,curl,intl,mysql,readline,xml,imagick,imap,mbstring,soap,zip,bcmath,redis}

# verify installation
php -v

Download Composer Latest: v2.6.6
To quickly install Composer in the current directory, run the following script in your terminal. To automate the installation, use the guide on installing Composer programmatically.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

This installer script will simply check some php.ini settings, warn you if they are set incorrectly, and then download the latest composer.phar in the current directory. The 4 lines above will, in order:

Download the installer to the current directory
Verify the installer SHA-384, which you can also cross-check here
Run the installer
Remove the installer
Most likely, you want to put the composer.phar into a directory on your PATH, so you can simply call composer from any directory (Global install), using for example:

sudo mv composer.phar /usr/local/bin/composer
For details, see the instructions on how to install Composer globally.

install redis

sudo apt install redis-server -y
sudo systemctl restart redis.service

redis-cli
# press ctrl+x for exit

install nodejs

## change the version to the LTS
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

node -v

install redis

```sh
npm i -g pm2

pm2 -v

install mysql

sudo apt install mysql-server

mysql
# run below commands in mysql prompt
CREATE DATABASE database_name;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'ANY_STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON * . * TO 'root'@'localhost';
FLUSH PRIVILEGES;
exit;

Assuming you are running your node js server on any specific port eg: 3000 from a path of the server eg: /var/www/your_code_folder

Nginx Conf File

Open default.conf file

nano /etc/nginx/sites-enabled/default.conf
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.php;

    server_name _;

    # to boost I/O on HDD we can disable access logs
    access_log off;

    gzip on;
    gzip_proxied any;
    gzip_types text/plain text/xml text/css application/x-javascript;
    gzip_vary on;
    gzip_disable “MSIE [1-6]\.(?!.*SV1);

    large_client_header_buffers 4 16k;

    # Node Server
    location ^~ / {
        proxy_pass http://localhost:3000;
        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;
    }

    # Socket.IO
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;

        proxy_pass http://localhost:9999;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location ^~ /socket.io {
        proxy_pass http://localhost:9999;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # Socket.IO specific settings
        proxy_set_header X-NginX-Proxy true;
        proxy_redirect off;
        proxy_buffering off;
    }

    # HTML code
    location ^~ /admin {
        alias /var/www/admin/;
        try_files $uri /admin/index.html;
    }


    # PHP (Laravel) - start
    location ^~ /api {
        alias /var/www/api/public;
        try_files $uri $uri/ @nested;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
    }

    location @nested {
        rewrite /api/(.*)$ /api/index.php?/$1 last;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
    # PHP (Laravel) - end


    # PHP Database Admin UI
    location ^~ /myphpmm1admin1 {
        alias /var/www/myphpmm1admin1/;

        try_files $uri $uri/ @nested;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

    }


}

service nginx restart

Visit localhost:8080 or localhost:80 and you should see Hello from Nginx Sever on browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment