First: you're in the wrong forum. This forum is all about porting new software, aka: trying to add it to the FreeBSD ports collection, a better choice would have been the
Web and Network Services forum. Don't get me wrong here: it's not the end of the world, but picking the right forum is also in your best interest because your question will get more attention from people interested in that same topic.
Aaaanyway....
For a simple purpose of learning and testing, I try to execute system commands in a virtual machine via cgi scripts in an apache server. For example: create a user or turn off and reboot the computer.
I just want to get an idea of how to do it, and maybe to create my own web services.
First of all be very careful here and ensure that you lock down your environment, otherwise it could be abused by others.
As to how to do this... A CGI script (Common Gateway Interface) is already by design a script which executes on the console. An often used approach is to use Perl for this, but other languages are usable too of course.
All you basically need to do is to
set up Apache with CGI support, then write the actual script (also explained on that same webpage).
Keep one important thing in mind though: your script will most likely be executed under the same process ID which the webserver uses (probably
www with UID
80 (see
/etc/passwd as well as your Apache configuration)). Which means that some system commands are most likely offlimits. Shutting down your server for example.
I suppose you could try to use
security/sudo to set this up; this would allow you to grant a specific user account to run specific commands while raising its security status to root.
Hope this gives you some ideas.