问题
I have a Linux server (VPS) with two domains and two web applications, one programmed with NodeJS
(chat app) whereas the another is PHP
.
So, what I want to do is running the two applications in separated domains on the same server. I've tried many methods to do this but all these methods working on single domain and multi URL path like : https://example.com
& https://example.com/node
.
But what I want is like this: https://nodeappexample.com
& https://apacheappexample.com
.
Greetings
回答1:
If you are on dedicated or VPS like DigitalOcean or any other server and planning to host multiple websites on a single server then here is how you can do it either with Apache HTTP or Nginx web server.
This assumes you have already mapped DNS to your host, like following two domains I have mapped to DigitalOcean.
bestflare.com
usefulread.com
Before you implement this, to give you an idea how it works. The concept to have multiple websites on a single instance is called Virtual Server. Virtual server configuration is defined within web server configuration and based on server/IP address, a request is getting forwarded to respective Document Root.
Configure Virtual Host in Apache to host multiple domains Login into Apache HTTP Server Go to apache conf location. ( in default installation – you will find it here /etc/httpd/conf/httpd.conf) Take a backup of httpd.conf file Create a VirtualHost container like below I have done for two domains.
<VirtualHost *:80>
ServerAdmin hello@chandank.com
DocumentRoot /opt/htdocs/bestflare
ServerName bestflare.com
ErrorLog logs/bestflare.com-error_log
CustomLog logs/bestflare.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin hello@chandank.com
DocumentRoot /opt/htdocs/usefulread
ServerName usefulread.com
ErrorLog logs/usefulread.com-error_log
CustomLog logs/usefulread.com-access_log common
</VirtualHost>
Note: Change the value for ServerAdmin, DocumentRoot, ServerName, ErrorLog, CustomLog based on your requirement.
Restart Apache HTTP and test both urls.
Configure Virtual Host in Nginx to host multiple domains Login into Nginx server Go to virtual.conf location (in default location – you will find it here /etc/nginx/conf.d/virtual.conf) Take a backup of virtual.conf Create server block for both URL’s as I have shown below.
server {
listen 80;
root /opt/htdocs/bestflare;
index index.html index.htm;
server_name bestflare.com;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
root /opt/htdocs/usefulread;
index index.html index.htm;
server_name usefulread.com;
location / {
try_files $uri $uri/ =404;
}
}
Note: Change the value for root, server_name based on your requirement.
Restart Nginx and test both URL’s That was the quick guide to hosting multiple domains in single web server instance like Apache or Nginx. Once your website is live, don’t forget to test it against security vulnerabilities.
来源:https://stackoverflow.com/questions/49946381/run-nodejs-and-apache-together-with-two-separated-domain