[Tutorial] Starting and stopping your server from a web browser
#1

This is designed to more or less integrate with a server UCP. However, it can be used to easily start and stop your server remotely, without losing any data or worrying about ssh.

The first step is to login to ssh and create a new user, using the function adduser. Then, install the server to /home/username (The name you just created). This will make things simple.

Now, all we've got to do is make the script, let's get started.

We're going to create the buttons to start, so open notepad and design as needed. This is an unstyled verstion. I'll make the code, and comment what each line does.
PHP Code:
<form method='POST' action='control.php'//you want the name to match the name of the php document, IE control.php.
<input type='submit' name="start" value='Start Server' /> //creates a button that will be used to start the server
<input type='submit' name="stop" value='Stop Server' />
</
form
This is extremely basic, and I would recommend using some sort of styling, or importing it to a user control panel.

But that's it for the html code, now we have to wire these buttons up. Let's add the api next. Create a folder called include, and paste the api found here: https://sampforum.blast.hk/showthread.php?tid=355574 inside. I renamed it to samp.php to make it easier.

Now, at the top of your html script, we will set everything up.

PHP Code:
<?php
include('include/samp.php');
if(
$_POST['start'])
{
}
if(
$_POST['stop'])
{
}
?>
This will set everything up. We've included the API, and the functions that are called when the buttons are pressed. Now, lets tell the script what to do when the buttons are pressed.

PHP Code:
if($_POST['start'])
{
$connection ssh2_connect('serverip'22); //port is usually 22.  change serverip to the ip, leaving the quotes.
ssh2_auth_password($connection'username''password'); //logging into the ssh, much like putty.  Pretty self explanatory on what to do
$stream ssh2_exec($connection'cd /home/username/samp; nohup ./samp03svr &'); //runs change directory (cd) command.  Set it to your server's location.  after the semicolon, it runs a separate command to start the server.
echo "Server started."//sends a message on the webpage saying the server was started.

Simple as that! Now, when that is pressed, the server should start up. Now, we have to stop the server.

PHP Code:
if($_POST['stop'])
{
$query = new SampRcon("ip"7777"rcon pass");  //fill in the credentials for the server.  this will allow connection to the rcon.
$query->call("say {FFFFFF}The server will be restarted. You will be kicked."1); //send a message in game to tell the players the server will be stopped.
$query->call("exit"2); //send the command /rcon exit.  will stop the server.
$query->close(); //close the connection
echo "Server stopped";

And there you have it. A basic start/stop system. Now, obviously, there are better ways to write this code. I would personally define the ips and passwords in a seperate config file, and style everything to look nice, however, I made this to be basic as possible so everyone can understand. If you have any questions, reply below. Any typos, I apologize in advance. I wrote this at 3am.
Reply
#2

Nice but... Whats about the website gets hacked? Even the Server will get hacked.
Reply
#3

Quote:
Originally Posted by Meller
View Post
Nice but... Whats about the website gets hacked? Even the Server will get hacked.
I guess this tutorial was designed just to show how you can manage a start/stop button for the server, not the security issues.

Anyways, nice tutorial! Never knew I could do this, I will definitely use this in future projects!
Reply
#4

Quote:
Originally Posted by LivingLikeYouDo
View Post
I guess this tutorial was designed just to show how you can manage a start/stop button for the server, not the security issues.
Well Newbies needs to learn the security things but yea. Its a good Start to learn PHP
Reply
#5

That's what I was looking for, this is time-saving and easy, thanks mate! +r
Reply
#6

Quote:
Originally Posted by Meller
View Post
Nice but... Whats about the website gets hacked? Even the Server will get hacked.
The tutorial is designed to show how to start and stop a server. You should either encrypt the page or put it with a UCP.
Reply
#7

Quote:
Originally Posted by TakeiT
View Post
The tutorial is designed to show how to start and stop a server. You should either encrypt the page or put it with a UCP.
Which doesn't really help with security, lol
Reply
#8

What about windows users? This doesn't appear to be windows-oriented at all. It's fine if you don't want to include a windows version; but it'd be great if you could include in the post somewhere that this isn't going to work on windows, and is designed with linux-only in mind.

Cheers.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)