A Web interface for the BeamMP multiplayer server
I'm not a developer. All the PHP and CSS code was written by ChatGPT (not without difficulty...) and I took care of the Shell / Mysql part. It is therefore highly probable (not to say certain) that the code contains security flaws, particularly in terms of SQL injections and verification of uploaded files. Any help I can get to improve the code is therefore most welcome. Thanks in advance to those who do.
This project has been designed to facilitate management of the BeamMP server by the administrator and trusted players. It is not intended for public access, and Apache authentication is strongly recommended.
• Displays the map currently configured on the server.
• Ability to change MAP
• Ability to delete an MOD
• Ability to upload an MOD
• Listing of all MODs present on the server (maps, cars and features) with the exception of the game's official cars.
• For each MOD (map, car, feature), its name, description and an image are displayed.
• For maps, an image showing the circuit layout can be displayed.
• Only the MAP currently configured on the server is present in the MOD folder, the others are placed in a separate folder to optimize MOD download and loading time.
• A button to update the BeamMP server with a single click!
• Multilingual management of the interface (and only the interface, not database content such as MOD names and descriptions). For the moment english, french, spanish and german only. Any language can be added to the lang file.
• Linux (I use Ubuntu 22.04 LTS)
• A Web server (I use Apache 2.4)
• PHP (I use 8.3 version)
• A database (I use MySQL 8.0)
• The following PHP modules must be installed : php-mysqli, php-json, php-fileinfo & php-filter
• The following Apache module must be installed : libapache2-mod-php
• Knowledge of using Linux from the command line, Apache, PHP and MySQL is a great advantage. Although I've tried to create a comprehensive installation guide, I don't go into detail about installing and configuring Apache, PHP or MySQL. You'll find a detailed tutorial here.
• Open the Web server port to make your site accessible on the Internet.
• A BeamMP server, even if it seems a bit obvious... (https://beammp.com/)
If you need help with the installation, don't hesitate to contact me on the dedicated Telegram channel: https://t.me/+VwELwQa3Cb8yMjhk
In our example, we'll use the following information:
- The root folder of the Web server where the Web interface files are located is: /var/www/html/
- The root folder of the BeamMP server is: /home/user/BeamMP/
- The database is called “gaming”.
- The table database is called “beamng_mod”.
- The default Apache user is www-data
- The BeamMP server service is called “BeamMP”.
So you need to modify the information to suit your environment.
Most operations require root access. It is therefore preferable to follow all the steps as a “root” user.
Click on the green “<> Code” button at the top of this page, then on “Download ZIP”.
Drop the contents of the “BeamMP-Web-Interface-main” folder into the root folder of your Web server (normally /var/www/html). You can also use a dedicated vhost (https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-18-04-quickstart).
In your BeamMP server folder, you'll find a “Resources” folder. In this folder, create a new folder called “inactive_map”. In the “Resources” folder, you should now have 3 folders: “Client”, “Server” and “inactive_map”.
cd /home/user/BeamMP/Resources
mkdir inactive_map
You must grant the necessary rights to the folders and files in the Web interface and BeamMP folder, so that Apache can access them to modify the configuration file and manipulate the MODs.
chmod -R 775 /var/www/html/beamng/ && chown -R www-data:www-data /var/www/html/beamng/ && chmod -R 775 /home/user/BeamMP/ && chown -R www-data:www-data /home/user/BeamMP/
chmod +x /var/www/html/beamng/scripts/changemap_script.sh && chmod +x /var/www/html/beamng/scripts/removemod_script.sh && chmod +x /var/www/html/beamng/scripts/uploadmod_script.sh
In the folder containing the beamng.php and config files, you'll find a file named .htaccess. This file restricts access to authorized users and protects the “config” file. You need to create an .htpasswd file outside your Web server folder to create user accounts for access to the Web interface:
To create the file for the first time (replace “username1” with the user name of your choice) :
htpasswd -c /var/www/.htpasswd username1
You will then be prompted to enter a password for the first user.
To add a new user to an existing .htpasswd file (replace “username2” with the username of your choice):
htpasswd /var/www/.htpasswd username2
You will then be asked to enter a password for this new user.
If you've created your .htpasswd file elsewhere, don't forget to change its path in the .htaccess file.
mysql
create database gaming;
exit
If you haven't already done so, you need to create a service for your BeamMP server. The changemap_script.sh script restarts the service each time the MAP is changed.
sudo nano /etc/systemd/system/BeamMP.service
Paste the following contents into the folder, adapting the path to the server execution script and the user and group running the server.
[Unit]
Description=Game server for BeamNG.drive
[Service]
ExecStart=/home/user/BeamMP/run.sh
Restart=always
User=user
Group=user
Environment=
[Install]
WantedBy=multi-user.target
(“ctrl x” then “y” to quit and save changes)
Start the service and activate it at system startup:
systemctl daemon-reload
systemctl start BeamMP
systemctl enable BeamMP
nano /var/www/html/beamng/config
Add or replace all the information in the configuration file to suit your environment:
WARNING: Do not put an equal sign (=) in the password, user name, database name or folder names.
-
servername = corresponds to the MySQL server name. Normally you'd leave this as “localhost”.
-
username = corresponds to the user name with access to the database
-
password = corresponds to the password of the user accessing the database
-
dbname = corresponds to the database name. If you haven't changed it in relation to this documentation, leave it as “gaming”.
-
tablename = Corresponds to the table name in the database. Useful if you want to host several instances.
-
BeamMPFolder = corresponds to the folder containing BeamMP server files
-
WebServerRootFolder = corresponds to the folder containing the Web interface files
WARNING : make sure the path begins and ends with a slash “/”.
- beammpservice = corresponds to the name of the BeamMP service we created in the previous step.
- lang = corresponds to the language of the Web interface.
I propose 4 SQL dumps in 4 different languages, allowing you to add the game's default maps to the database:
WARNING (optionnal) : If you have changed the name of the default table used in this documentation (beamng_mod) and/or wish to host multiple instances, change the table name with a sed command before importing the maps. Replace “new_table_name” with the name of the table you want to use.
cd /var/www/html/
sed -i 's/beamng_mod/new_table_name/g' chemin/vers/gaming_EN.sql
Import the mySQL dump containing the default maps :
cd /var/www/html/
mysql gaming < gaming_EN.sql
(Replace “EN” with “FR”, “DE” or “ES” depending on the language you require).
In the database, the default map configured in the dump we loaded above is driver_training. It is therefore necessary to configure this map in the config file once for the MAP change script to work:
nano /home/user/BeamMP/ServerConfig.toml
at Map level = configure the correct MAP, so the line looks like this:
Map = “/levels/driver_training/info.json”
To enable the user running Apache to execute scripts and commands without having root rights (which requires entering a root password), you need to add the commands he is authorized to execute to the sudoer file
sudo visudo
Add the following lines to the file :
www-data ALL=NOPASSWD: /bin/systemctl restart BeamMP.service
www-data ALL=NOPASSWD: /bin/systemctl start BeamMP.service
www-data ALL=NOPASSWD: /bin/systemctl stop BeamMP.service
www-data ALL=NOPASSWD: /bin/chmod +x /home/user/BeamMP/BeamMP-Server.ubuntu*
www-data ALL=NOPASSWD: /var/www/html/beamng/scripts/changemap_script.sh
www-data ALL=NOPASSWD: /var/www/html/beamng/scripts/removemod_script.sh
www-data ALL=NOPASSWD: /var/www/html/beamng/scripts/updateserver_script.sh
www-data ALL=NOPASSWD: /var/www/html/beamng/scripts/uploadmod_script.sh
Warning: don't forget to replace the BeamMP service name, the BeamMP server folder /home/user/BeamMP/ and the Web server folder /var/www/html/ with whatever is appropriate for your system.
That's it, your Web interface is up and running. Don't forget to open the Apache server port if you want to make it accessible to other trusted players.
Here are the things I'd like to add. But I don't promise to ever do it (especially the installation script)
• When uploading a MAP, automatically detect the MAP ID to update the BeamMP server config file.
• Creating an installation script
• Support for MAPs with multiple variants in the zip file. For the moment it's possible to do this by manually inserting the other variants in the database (or perhaps by uploading the same MAP several times by changing its ID in the MOD upload form. But I haven't tried).