简体   繁体   English

如何在一个域中配置Apache服务器和节点服务器?

[英]How to configure Apache server and node server in one domain?

I had trouble studying.我学习有困难。

I'm making my own sns app like Facebook or Instagram.我正在制作自己的 sns 应用程序,例如 Facebook 或 Instagram。 So I completed the timeline feature, so this time I'm going to create a chat.所以我完成了时间线功能,所以这次我要创建一个聊天。 So I want to make a chat function using node.js + socket.io.所以我想用node.js + socket.io做一个聊天功能。 That's why I wonder how to build a node.js server.这就是为什么我想知道如何构建 node.js 服务器。

My development environment is Ubuntu 18.04 + apache2 + Mysql (+ Let 's Encrypt SSL And I have a domain)我的开发环境是 Ubuntu 18.04 + apache2 + Mysql (+ Let's Encrypt SSL And I have a domain)

Even if I searched on google and looked up I couldn't create an https node.js server.即使我在谷歌上搜索并查找,我也无法创建 https node.js 服务器。

Can't someone help me easily and in detail?有人不能轻松详细地帮助我吗?

android is from Api 29 https communication is recommended by default. android 来自 Api 29 默认推荐使用 https 通信。 You can communicate with http on api 29 but that's not a long term solution.您可以在 api 29 上与 http 通信,但这不是一个长期的解决方案。 That's why I configured my server with https.这就是我使用 https 配置我的服务器的原因。 I will never change it to http.我永远不会把它改成http。

Assume the Domain is called test.com.假设域名为 test.com。

The following is the setting in my 000-default.conf file.以下是我的 000-default.conf 文件中的设置。

vi /etc/apache2/sites-availabls/000-default.conf vi /etc/apache2/sites-availabls/000-default.conf

<VirtualHost *:80>
    ServerName test.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    Redirect / https://test.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName  test.com
    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateKeyFile /etc/letsencrypt/live/test.com/privkey.pem
    SSLCertificateFile /etc/letsencrypt/live/test.com/cert.pem
    SSLCACertificateFile /etc/letsencrypt/live/test.com/fullchain.pem

<Directory />
    AllowOverride All
    Options All -Indexes
    Require all granted
</Directory>
</VirtualHost>

Port 80 is known as http communication.端口 80 称为 http 通信。 So I redirected to https uri.所以我重定向到https uri。

So I basically know that port 443 is the port for https communication.所以我基本知道443端口是https通信的端口。 Anyway, the important thing is that my Apache server is using port 80 and port 443. Here we want to create a node.js socket server that can chat with https communication.无论如何,重要的是我的Apache服务器使用的是80端口和443端口。这里我们要创建一个可以用https通信聊天的node.js socket服务器。 It would be really helpful if you helped me easily and in detail.如果您能轻松而详细地帮助我,那将非常有帮助。

Root path of the server -> /var/www/html/服务器的根路径 -> /var/www/html/

php folder path to use in android -> /var/www/html/android在android中使用的php文件夹路径-> /var/www/html/android

User profile picture folder path to use on android -> /var/www/html/android/image要在 android 上使用的用户个人资料图片文件夹路径 -> /var/www/html/android/image

I want to create a node.js file in the following path.我想在以下路径中创建一个 node.js 文件。

File folder for chatting on android.用于在 android 上聊天的文件夹。 -> /var/www/html/android/chat -> /var/www/html/android/chat

I searched on Google but it all failed.我在谷歌上搜索,但都失败了。 Otherwise, all the examples I saw ran only one node.js server.否则,我看到的所有示例都只运行了一个 node.js 服务器。

I tried proxypass or proxypassreverse or Something but Failed.我尝试了 proxypass 或 proxypassreverse 或Something but Failed。

Try running your server on any free port on your server machine尝试在服务器计算机上的任何空闲端口上运行服务器

var https = require('https');
https.createServer(options, app).listen(ANY_PORT);

and in your android code specify the port to connect to并在您的 android 代码中指定要连接的端口

val url = URL("https://test.com:ANY_PORT")

That works for me.这对我行得通。

One thing you can do is have a reverse proxy with apache.您可以做的一件事是使用 apache 进行反向代理。 See the docs: https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html请参阅文档: https : //httpd.apache.org/docs/2.4/howto/reverse_proxy.html

What this really means is that say your php stuff is running on port 80 and 443. However, now you have a node service running on port 3000.这真正意味着说你的 php 东西在端口 80 和 443 上运行。 但是,现在你有一个节点服务在端口 3000 上运行。

Though, you want to mask port 3000 as port 80. So you can say you have domain.com/chat running your port.但是,您想将端口 3000 屏蔽为端口 80。所以您可以说您有 domain.com/chat 运行您的端口。 So you listen for any routes that hit that path and forward them off to the application running that service.因此,您可以侦听到达该路径的任何路由,并将它们转发到运行该服务的应用程序。

Install Letsencrypt from certbot.eff.org by following the instructions and use this command.按照说明从 certbot.eff.org 安装 Letsencrypt 并使用此命令。 Select the No Redirect option at the installation.安装时选择No Redirect选项。

sudo certbot --apache

Copy the privkey.pem and fullchain.pem from your certificate folder using the below command使用以下命令从您的证书文件夹中复制privkey.pemfullchain.pem

sudo cp /etc/letsencrypt/live/test.com/privkey.pem ~/nodejs-rootfolder
sudo cp /etc/letsencrypt/live/test.com/fullchain.pem ~/nodejs-rootfolder

nodejs-rootfolder is where you have kept the Node.js server project nodejs-rootfolder是您保存Node.js服务器项目的地方

Mention the below code in your index.js or server.js file.index.jsserver.js文件中提及以下代码。

var express = require('express');
var http = require('http');
var https = require('https');
var app = express();

app.use(cors())
app.use(bodyParser.json({ limit: '500mb' }))
app.use(bodyParser.urlencoded({ extended: true }))
var fs = require('fs');

var privateKey = fs.readFileSync('privkey.pem');
var certificate = fs.readFileSync('fullchain.pem');

var credentials = { key: privateKey, cert: certificate };
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
app.all('*', function (req, res, next) {
   res.set('Access-Control-Allow-Origin', req.header('origin') || req.header('x- forwarded-host') || req.header('referer') $
   res.set('Access-Control-Allow-Credentials', true);
   res.set('Access-Control-Allow-Methods', 'POST,GET,PUT,DELETE');
   res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization,Access-control-request-headers,i$
   if ('OPTIONS' == req.method) return res.send(200);
   next();
});
var port = process.env.PORT || 5001;
httpsServer.listen(port, () => console.log(`Listening on port ${port}`));

The certbot --apache will do all the installations and setup for SSL configuration. certbot --apache将完成 SSL 配置的所有安装和设置。 With the above-listed workaround, you can get it done.使用上面列出的解决方法,您可以完成它。 However, if you want your server to redirect all HTTP to HTTPS then use the below config.但是,如果您希望您的服务器将所有HTTP重定向到HTTPS使用以下配置。

sudo nano /etc/apache2/sites-enabled/000-default.conf

<VirtualHost *:80>
    ServerName test.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =test.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</VirtualHost>

Then perform sudo service apache2 restart然后执行sudo service apache2 restart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM