DevOps

Как добавить в NGINX новый домен на VPS

Create new site on VPS

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/newsite
sudo nano /etc/nginx/sites-available/newsite
server {
	listen 80;
	listen [::]:80;

	# SSL configuration
		listen 443 ssl;
		listen [::]:443 ssl;

		include snippets/self-signed.conf;
	#

	root /var/www/newsite.net/html/public;
	index index.php index.html index.htm index.nginx-debian.html;

	server_name newsite.net www.newsite.net;

		location / {
			try_files $uri $uri/ /index.php?$query_string;
		}

		location ~ \.php$ {
				include snippets/fastcgi-php.conf;
				fastcgi_pass unix:/run/php/php7.4-fpm.sock;
		}
}
  • Create symlink to sites-enabled:
sudo ln -s /etc/nginx/sites-available/newsite /etc/nginx/sites-enabled/
  • Config your DNS server:

 Main -> Domain names -> Your domain name -> Records -> Create -> 
  Name  -> Your domain name
  Type   -> A
  IP-adress -> Your IP adress

  • (optional) Edit hosts file on Your working machine (only if your DNS still not configured):

open C:\Windows\System32\drivers\etc\hosts

11.22.33.44 newsite.ru
  • Restart nginx on VPS:
sudo systemctl restart nginx
  • Try to open your site: newsite.ru

 

Add host only for local use for IP without domain name:

 cp /etc/nginx/sites-available/default /etc/nginx/sites-available/newsite

 

sudo nano /etc/nginx/sites-available/newsite
server {
   listen THIS_SERVER_IP;
   server_name NEW_VIRTUAL_DOMAIN_NAME;
   root /var/www/this_project/html/public;

   add_header X-Frame-Options "SAMEORIGIN";
   add_header X-XSS-Protection "1; mode=block";
   add_header X-Content-Type-Options "nosniff";

   index index.html index.htm index.php;

   charset utf-8;

   location / {
    try_files $uri $uri/ /index.php?$query_string;
   }

   location = /favicon.ico { access_log off; log_not_found off; }
   location = /robots.txt  { access_log off; log_not_found off; }

   error_page 404 /index.php;

   location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    include fastcgi_params;
   }

   location ~ /\.(?!well-known).* {
    deny all;
   }
  }

 - Create symlink to sites-enabled:

 sudo ln -s /etc/nginx/sites-available/newsite /etc/nginx/sites-enabled/

- Edit hosts file on Your working machine (only if your DNS still not configured):
 open C:\Windows\System32\drivers\etc\hosts

THIS_SERVER_IP NEW_VIRTUAL_DOMAIN_NAME

- Restart nginx on VPS:

sudo systemctl restart nginx

Leave a reply