Change nginx settings when installing from script installer

I am writing a script installer for my php application, which is based on laravel. It will be deployed to the lemp stack, and for the installation to work, I need to make these changes to the nginx settings:

  • Replace all public_html with public_html/public in the file /etc/nginx/sites-available/$domain.conf
  • Add the following code line: location / { try_files $uri $uri/ /index.php$is_args$args; }

This is what I am doing currently in the script_name_install:

$domain = &get_domain_http_hostname($d); local $nxcfg = "/etc/nginx/sites-available/$domain.conf";
$public = "public_html\\/public";
$php = 'location \/ { try_files \\$uri \\$uri\/ \/index.php\\$is_args\\$args; } \\n\\t';

# Nginx settings
local 	$cmd = "sed -i \"s/public_html/$public/g\" $nxcfg; ";
		$cmd .= "sed -i \"s/root/$php root/g\" $nxcfg; ";
		$cmd .= "nginx -s reload";


but somehow the nginx config file getting messed up AFTER my changes, something like missing brackets or it has now two lines of fastcgi_read_timeout like:

fastcgi_read_timeout 60; # This is the original
fastcgi_read_timeout 9999; # This is being added later, no idea why!

So my question is: what is the best way to change the nginx settings please?

Also: where can I change the template of the nginx config for the domains? And can I have multiple config files for multiple hosting plans?

Thank you!