#7 First deploymentOctober 21, 2022
Let's copy our
server folder to our server at
api.jurajmajerik.com. We can do this with the
scp command. Add the
-r option to copy the entire directory recursively.
scp -r server firstname.lastname@example.org:~
When we log in to our server, we see the folder has been copied successfully.
There is an issue in our server code. It currently starts the server at
8080, which is a local port. We can't connect to this port from the outside. So if we want to expose our server to the world - i.e., allow anyone to make HTTP requests to it, we need to start the server at port
So while still on our server, let's open the server code in an editor.
Change the port from
80 and save the file.
server and start it with
go run main.go. The server, however, fails to start. That's because, for security reasons, ports below
1024 require root privileges. Therefore, we need to run the server with
sudo command has its own
$PATH, which is defined in the sudo config. To add
go to that path, do the following:
Open the sudo config file:
Find the line starting with
Add the following to the end of the line:
Start the server:
sudo go run main.go
Now the world can make requests to our server!
One more thing. As soon as we log out, the program also stops. When we log out or disconnect from the remote server, the terminal session is terminated, along with any processes running within that session. To solve this, we need to run the command in the background. This is done by adding the
& character after the command.
juraj@server:~/server$ sudo go run main.go & 2200302
We get back the process ID (PID):
2200302. With a
ps -e command, we can show all running processes in the system.
...2200302 pts/1 00:00:00 sudo2200303 pts/1 00:00:00 go2200330 pts/1 00:00:00 main...
For a reason I don't yet understand, our command seems to have started three processes:
main. I'll update this section once I have the explanation for this.
sudo runs a seprate process for reasons described in this superuser question.
go run compiles your code, executes the resulting binary, and then cleans up the results of compilation. In order to do the cleanup afterwards it needs to use a
fork() model since
exec() would not remember to do the cleanup (somewhat similar to the above).
We can stop a running process with a
kill <PID>. To stop our server, we need to target the
juraj@server:~/server$ sudo kill 2200330signal: terminated+ Exit 1 sudo go run main.go