1

I have a server administration page coded in PHP, and one of the features is to change the port on which the admin page is accessed. This is done with a number <input> field in a self-calling form, POST method. When the Submit button is clicked, the code takes the input number, writes it to the Nginx config file for the server as the port number to use, then runs the command to restart Nginx:

exec('sudo service nginx restart > /dev/null &');

The problem is that when this command is run, Firefox throws up a Content error. I have tried to work around the problem, changing to this block of code:

$newAPort = $_POST['admin-port'];
header('Content-Length: 0');
// Restart Nginx
exec('sudo service nginx restart > /dev/null &');
sleep(15);
$newURL = 'http://'.$_SERVER[HTTP_HOST].':'.$newAPort;
header('Location: '.$newURL);

That's some progress, the Firefox error is now "Unable to connect", and the error shows the URL with the old port number. How can I redirect to the new port correctly? I thought the above block of code would do it but it doesn't.

X-Mann
  • 327
  • 2
  • 5
  • 15
  • 1
    Side note: if `$_SERVER[HTTP_HOST]` doesn't display a notice, it means you are coding PHP with error reporting disabled. That makes it harder than it should. – Álvaro González Jun 08 '17 at 09:32
  • You are expecting a PHP script that was started by the web server to still execute after the web server gets restarted in between …? I don’t think this is possible at all this way. – CBroe Jun 08 '17 at 09:34
  • @Alvaro - Good point, but until today (about to deliver to client) I had error reporting on and there was no notice. What should the notice be? – X-Mann Jun 08 '17 at 09:35
  • @CBroe - I believe it is possible because I've been told that some firewall admin pages enable port changing, and after a delay, use that port. – X-Mann Jun 08 '17 at 09:36
  • 1
    The notice should be _“Notice: Use of undefined constant HTTP_HOST - assumed 'HTTP_HOST' in […]”_ unless you actually defined a constant by the name HTTP_HOST yourself somewhere. – CBroe Jun 08 '17 at 09:38
  • 2
    _“I've been told that some firewall admin pages enable port changing, and after a delay, use that port”_ – I still doubt they would achieve that in exactly this way ... // I would probably return a complete HTML document to the client first, perhaps [close the connection](https://stackoverflow.com/questions/138374/close-a-connection-early), and then restart the web server. Said HTML document you output before would contain a meta refresh to make the browser request the new address with the new port after X seconds ... – CBroe Jun 08 '17 at 09:41
  • @CBroe - That sounds like a workable idea, I'll try it and report back, though it might take me a while. – X-Mann Jun 08 '17 at 09:45

1 Answers1

1

The discussion with CBroe led in the right direction, and after a few more searches on SO and elsewhere, I got a working solution, posted here in case it helps others. The block of code in my question is refactored like so:

$rawHost = $_SERVER['HTTP_HOST']; // Get host
$onlyHost = explode(':', $rawHost)[0]; // Drop port num
$newAPort = $_POST['admin-port'];
header('Refresh:5; url=http://'.$onlyHost.':'.$newAPort, true, 301);
echo '<h3>Please wait a few seconds while the server updates...</h3>';
fastcgi_finish_request();
// Restart Nginx
exec('sudo service nginx restart > /dev/null &');
X-Mann
  • 327
  • 2
  • 5
  • 15