大网数据广告活动
网站运营与故障问答

NodeJS app is deployed to port 8080

· 52CE 我爱测

This might be a very simple question but I don't know what to search to solve my problem. Please feel free to direct me to the right resource, that would also be very helpful. This is my first time trying to deploy an app from No…

问题原文

原帖链接 · Leonardo Petrucci · CC BY-SA 4.0

This might be a very simple question but I don't know what to search to solve my problem. Please feel free to direct me to the right resource, that would also be very helpful.

This is my first time trying to deploy an app from Node.js to an actual server (I've only ever tested locally).

My app is built with NextJS. I've successfully built it on my server and then ran:

npm run start

The app seems to be running perfectly, however it's currently running on my domain on port :8080, which means that I can only access it from my domain like this example.com:8080.

I've tried forcing it to run on port 80 but that port is already in use.

I've tried making Apache listen to port 8080 following this guide, but then my app refuses to start because the port is also already in use.

How do I make my app appear on example.com? Is it something I have to do from my domain DNS settings?

Is it possible to also still run Apache on a different port?

已采纳解答

原帖链接 · Leonardo Petrucci · CC BY-SA 4.0

I ended up messing around a lot with this and have a solution. I am unsure if it's a good solution, but it's a solution nonetheless.


The problem

The biggest problem here is that I need to run both a NodeJS app AND Wordpress at the same time. Wordpress needs to be accessible on a public address so that my app can access it.

As of yet I have not found a way to run Wordpress on a different port. I do not know if it is possible. Apache can be run on different ports, but that seems to be a global setting that would affect all sites running on the server.

My solution

My solution is pretty simple. I created yet another Apache virtual host situated in a subdomain api.example.com and moved my Wordpress installation to it.

This way I don't have to worry about my app displaying on port :80 over my Wordpress installation.

I then ran my app on my base domain example.com on port :8080 and modified my Apache Virtual Host by editing etc/apache2/sites-available/example.com.conf with this code:

<VirtualHost *:80>

        ServerAdmin [email omitted]
        ServerName example.com

       DocumentRoot /var/www/example.com/public_html
        <Directory />
        Options -Indexes +FollowSymlinks
        AllowOverride None
        Require all granted
        </Directory>

       ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
        <Proxy *>
        Require all granted
        </Proxy>

       <Location />
        ProxyPass http://127.0.0.1:8080/
        ProxyPassReverse http://127.0.0.1:8080/
        </Location>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

The important part here is this:

       <Location / >
        ProxyPass http://127.0.0.1:8080/
        ProxyPassReverse http://127.0.0.1:8080/
        </Location>

What it does is take whatever is on port :8080 and instead display it on port :80 which we set at the start <VirtualHost *:80> and at the root of the domain <Location / >.

This could be written as <Location /app > if you wanted to display your app at example.com/app.

Bear in mind I am not an expert with Apache and I just put this answer together from about 10 or so sites that contained only part of what I needed. The explanation of how this works could be wrong, this is just my understanding of it. Feel free to correct me if it is wrong.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-07-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。