1. Apache installieren
sudo yum -y install httpd
2. Bevor wir Apache konfigurieren, machen wir ein Backup der Konfigurationsdatei
cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup
/etc/httpd/conf/httpd.conf
3. Virtual Host einrichten
Document-Ordner anlegen
sudo mkdir -p /var/www/project/public_html
Eigentümer der Ordners anpassen
sudo chown -R $USER:$USER /var/www/project/public_html
Rechte der Ordner anpassen
sudo chmod -R 755 /var/www
Es gibt mehrere Möglichkeiten Virtuelle Host zu steuern, wir
arbeiten mit 2 Ordner "sites-available" und "sites-enabled",
um unsere Projekte zu aktivieren oder zu deaktivieren.
sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled
Apache soll die Konfiguration kennen
sudo nano /etc/httpd/conf/httpd.conf
Am Ende der apache-conf hinzufügen
IncludeOptional sites-enabled/*.conf
Virtual-Host erstellen
sudo nano /etc/httpd/sites-available/project.conf
<VirtualHost *:80>
ServerName www.project.com
ServerAlias project.com
DocumentRoot /var/www/project.com/public_html
ErrorLog /var/www/project.com/error.log
CustomLog /var/www/project.com/requests.log combined
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:<PORT>/
ProxyPassReverse http://localhost:<PORT>/
</Location>
</VirtualHost>
Symbolischen Link erstellen in sites-enabled
sudo ln -s /etc/httpd/sites-available/project.conf /etc/httpd/sites-enabled/project.conf
Apache neu starten
sudo apachectl restart
Now let's understand what is going on here. First you're telling Apache to use this given location of the ProxyPass if someone is requesting something from port 80 instead of given them the specific file in your server instead you are redirecting them to the actual IP:PORT which you used to access before
4. Editieren der /etc/hosts
127.0.0.1 localhost
127.0.1.1 guest-desktop
99.99.99.99 project.de www.project.de
5. Aktivere Apache-Dienste nach dem Booten und Reload der falls Änderungen gemacht wurden
sudo systemctl enable httpd.service
sudo systemctl restart httpd.service
6. Create Nodejs Server erstellen und dann als Service starten
Create the node.js server whereever you want, perhaps in your Document-Root /var/www/projectconst http = require('http'); const hostname = '0.0.0.0'; // listen on all ports const port = 1337; http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n'); }).listen(port, hostname, () => {
console.log(Server running at http://${hostname}:${port}/`); });
Create Service File
nano /etc/systemd/system/nodeserver.service [Unit] Description=Node.js Example Server #Requires=After=mysql.service # Requires the mysql service to run first [Service] ExecStart=/usr/bin/node /opt/myserver/server.js # Required on some systems #WorkingDirectory=/opt/nodeserver Restart=always # Restart service after 10 seconds if node service crashes RestartSec=10 # Output to syslog StandardOutput=syslog StandardError=syslog SyslogIdentifier=nodejs-example #User=<alternate user> #Group=<alternate group> Environment=NODE_ENV=production PORT=1337 [Install] WantedBy=multi-user.target
Enable the Service
systemctl enable nodeserver.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nodeserver.service to /etc/systemd/system/nodeserver.service. Start Service
systemctl start nodeserver.service
Service really started?
systemctl status nodeserver.service
● nodeserver.service – Node.js Example Server
Loaded: loaded (/etc/systemd/system/nodeserver.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2019-12-22 16:20:10 CET; 26min ago
Main PID: 9015 (node)
Tasks: 6
Memory: 8.1M
CGroup: /system.slice/nodeserver.service
└─9015 /usr/bin/node /opt/nodeserver/server.js
Dec 22 16:20:10 vmd20xxx.contaboserver.net systemd[1]: Started Node.js Example Server.
Dec 22 16:20:11 vmd20xxx.contaboserver.net nodejs-example[9015]: Server running at http://0.0.0.0:1337/
Security / testing
Of course this service would run as root, which you probably shouldn’t be doing, so you can edit /etc/systemd/system/nodeserver.service to run it as a different user depending on your system. If you make any changes to the service file, you will need to do a
systemctl daemon-reload
before reloading the service:
systemctl restart nodeserver.service
I always suggest doing a systemctl status nodeserver.service after edits and a restart to ensure the service is running as expected.
Now finally we can test how systemd restarts the process if it unexpectedly dies (thanks Mark for the suggestion). We will manually „kill“ the running process by finding its Process ID (pid), and note how systemd automatically restart it again:
ps -ef | grep server.js # find the current pid
kill 12345 # kill the process by its pid reported above
ps -ef | grep server.js # notice node process is immediately respawned with a different pid
Quelle für Nodejsserver als Service
https://www.axllent.org/docs/view/nodejs-service-with-systemd/