Install Ngnix on EC2 server from amazone, Configurate domain, sub-domain and Cloudflare

  1. Register EC2.

https://ap-southeast-1.console.aws.amazon.com/

choose “Services” and find “EC2”

Create Linux Host: “Launch Instance” > “Launch Instance”

Result:

2. Install Ngnix

3.Configure Ngnix

Must clone your source code to the folder: /var/www

Get implement folder having index file for configuration file(next step)

For example:

For each source, create a configuration file in /etc/nginx/sites-available

4000: port that you want to access

3.0.16.73: IP v4 public of host

/var/www/emptyprojectreactcss/frontend/build folder have index.html file

/var/log/nginx/emptyprojectreactcss.access.log log file(You don’t need to create it, just define there)

Create a symbol link file on /etc/nginx/site-enabled/

Check our configuration: sudo nginx -t

Open port 4000 on EC2

Restart Ngnix service

We have our web

4. Get domain and configure cloudflare

You can get domain of any page you want

Here I have an exmaple domain on : https://dcc.godaddy.com/

Register an account on cloudflare: https://dash.cloudflare.com/

after register, add your domain that you have

follow all step and change Nameserver that cloudflare provide on ddc.godaddy

after change successfully

You can add sub-domain and point to another port of this host

edit file: :/etc/nginx/nginx.conf

edit :/etc/nginx/sites-available

server {
listen 443 ssl;
server_name domain.thatran.site;
listen [::]:443 ssl http2;
ssl on;
ssl_certificate /etc/ssl/certs/thatran.pem;
ssl_certificate_key /etc/ssl/private/thatran.pem;
ssl_client_certificate /etc/ssl/certs/origin-pull-ca.pem;
ssl_verify_client on;

root /var/www/domain/build;
index index.html;
access_log /var/log/nginx/domain.access.log;
error_log /var/log/nginx/domain.error.log;

location / {
try_files $uri /index.html =404;
}
}

to add https

For PHP host

server {
listen 443 ssl http2;
server_name d4003.duan-akair.com;
root /var/www/d4003/wordpress;
index index.php;
# SSL parameters
ssl_certificate /etc/ssl/certs/akair.pem;
ssl_certificate_key /etc/ssl/private/akair.pem;
ssl_trusted_certificate /etc/ssl/certs/origin-pull-ca.pem;
# log files
access_log /var/log/nginx/sample.com.access.log;
error_log /var/log/nginx/sample.com.error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
}

if you must use html folder to load

server {
listen 3000;
server_name nhadatphucan.com;

root /var/www/test/build;
index index.html;
access_log /var/log/nginx/test.access.log;
error_log /var/log/nginx/test.error.log;

location / {
try_files $uri /index.html =404;
}
}