{"id":22951569,"url":"https://github.com/lifailon/bash-api-server","last_synced_at":"2025-04-16T02:59:15.843Z","repository":{"id":224435996,"uuid":"763246143","full_name":"Lifailon/bash-api-server","owner":"Lifailon","description":"REST API (HTTP) server based on Apache and backend Bash via cgi module for remote managment Linux using curl or Invoke-WebRequest in Windows.","archived":false,"fork":false,"pushed_at":"2024-02-29T12:58:32.000Z","size":924,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"rsa","last_synced_at":"2025-04-15T07:19:05.892Z","etag":null,"topics":["api","api-server","bash","bash-script","curl","http-server","linux","rest-api","rest-server","server","shell","shell-script","web"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lifailon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-02-25T23:01:15.000Z","updated_at":"2025-03-06T08:17:20.000Z","dependencies_parsed_at":"2024-02-27T13:43:19.217Z","dependency_job_id":null,"html_url":"https://github.com/Lifailon/bash-api-server","commit_stats":null,"previous_names":["lifailon/shell-api-server","lifailon/bash-api-server"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lifailon%2Fbash-api-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lifailon%2Fbash-api-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lifailon%2Fbash-api-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lifailon%2Fbash-api-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lifailon","download_url":"https://codeload.github.com/Lifailon/bash-api-server/tar.gz/refs/heads/rsa","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249188368,"owners_count":21227013,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-server","bash","bash-script","curl","http-server","linux","rest-api","rest-server","server","shell","shell-script","web"],"created_at":"2024-12-14T15:16:50.265Z","updated_at":"2025-04-16T02:59:15.824Z","avatar_url":"https://github.com/Lifailon.png","language":"Shell","readme":"# Bash API Server\n\nREST API server based on Apache and **backend Bash via cgi module** for remote managment Linux 🐧 using curl or Invoke-WebRequest in Windows.\n\nImplemented Linux service management via **systemd** (with `systemctl` commands) using **basic authorization**.\n\n## 🚀 Install\n\nInstall an **Apache server** and [jqlang](https://github.com/jqlang/jq) for `json` processing (in the example for Ubuntu/Debian):\n\n```Bash\napt install apache2 jq\n```\n\n### Build\n\nThe following described customization steps can be performed using the [build](https://github.com/Lifailon/bash-api-server/blob/rsa/build.sh) script.\n\n\u003e 💡 Sudo privileges required\n\n1. Configure port `8443` (or use any other by specifying it in `VirtualHost`):\n\n```Bash\ncat /etc/apache2/ports.conf | sed -r \"s/^Listen.+/Listen 8443/\" \u003e /etc/apache2/ports.conf\n```\n\n2. Activate the HTTP `Basic Authentication` module and add a user (in the example `rest` and password `api`):\n\n```Bash\na2enmod auth_basic\nhtpasswd -b -c /etc/apache2/.htpasswd rest api\n```\n\n3. Create a file at the path `/var/www/api/api.sh` and copy the contents of the `api.sh` script:\n\n```Bash\nmkdir /var/www/api \u0026\u0026 touch /var/www/api/api.sh \u0026\u0026 chmod +x /var/www/api/api.sh\ncurl -s \"https://raw.githubusercontent.com/Lifailon/bash-api-server/rsa/www/api/api.sh\" \u003e /var/www/api/api.sh\n```\n\n4. Configure a **VirtualHost** (`/etc/apache2/sites-available/api.conf`) this way:\n\n```Bash\n\u003cVirtualHost *:8443\u003e\n    ScriptAlias /api /var/www/api/api.sh\n    \u003cDirectory \"/var/www/api\"\u003e\n        Options +ExecCGI\n        AddHandler cgi-script .sh\n        AllowOverride None\n        Require all granted\n    \u003c/Directory\u003e\n    \u003cLocation \"/api\"\u003e\n        AuthType Basic\n        AuthName \"Restricted Area\"\n        AuthUserFile /etc/apache2/.htpasswd\n        Require valid-user\n        SetHandler cgi-script\n        Options +ExecCGI\n    \u003c/Location\u003e\n    ErrorLog ${APACHE_LOG_DIR}/error.log\n    CustomLog ${APACHE_LOG_DIR}/access.log combined\n\u003c/VirtualHost\u003e\n```\n\n5. Activate the module for working cgi-scripts, activate the created VirtualHost and start the server:\n\n```Bash\na2enmod cgi\na2ensite api.confs\nsystemctl restart apache2\n```\n\n6. In order for the Apache server to be able to manage services, the `www-data` user must be granted the appropriate sudo permissions:\n\n```Bash\necho \"www-data ALL=(ALL) NOPASSWD: /bin/systemctl start *, /bin/systemctl stop *, /bin/systemctl restart *\" \u003e\u003e /etc/sudoers\n```\n\n## 📑 Examples\n\nGet the status of the service specified in the header (the example, cron):\n\n`curl -s -X GET http://192.168.3.101:8443/api/service/cron -u rest:api`\n\n```json\n{\n  \"unit\": \"cron.service\",\n  \"load\": \"loaded\",\n  \"active\": \"active\",\n  \"sub\": \"running\",\n  \"description\": \"Regular background program processing daemon\",\n  \"uptime\": \"5h 1min\",\n  \"startup\": \"enabled\"\n}\n```\n\nStop the service:\n\n`curl -s -X POST http://192.168.3.101:8443/api/service/cron -u rest:api -H \"Status: stop\"`\n\n```json\n{\n  \"unit\": \"cron.service\",\n  \"load\": \"loaded\",\n  \"active\": \"inactive\",\n  \"sub\": \"dead\",\n  \"description\": \"Regular background program processing daemon\",\n  \"uptime\": \"108ms\",\n  \"startup\": \"enabled\"\n}\n```\n\nStart the service:\n\n`curl -s -X POST http://192.168.3.101:8443/api/service/cron -u rest:api -H \"Status: start\"`\n\n```json\n{\n  \"unit\": \"cron.service\",\n  \"load\": \"loaded\",\n  \"active\": \"active\",\n  \"sub\": \"running\",\n  \"description\": \"Regular background program processing daemon\",\n  \"uptime\": \"104ms\",\n  \"startup\": \"enabled\"\n}\n```\n\nPowerShell (output in default object format):\n\n```PowerShell\n$user = \"rest\"\n$pass = \"api\"\n$EncodingCred = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(\"${user}:${pass}\"))\n$Headers = @{\"Authorization\" = \"Basic ${EncodingCred}\"}\nInvoke-RestMethod -Headers $Headers -Uri http://192.168.3.101:8443/api/service/cron\n\nunit        : cron.service\nload        : loaded\nactive      : active\nsub         : running\ndescription : Regular background program processing daemon\nuptime      : 41min\nstartup     : enabled\n```\n\n![Image alt](https://github.com/Lifailon/bash-api-server/blob/rsa/image/console-output.jpg)\n\nList of services in **HTML table format** when accessed through a Web browser Chrome:\n\n![Image alt](https://github.com/Lifailon/bash-api-server/blob/rsa/image/service-list-html-table.jpg)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flifailon%2Fbash-api-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flifailon%2Fbash-api-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flifailon%2Fbash-api-server/lists"}