How to properly launch Angular Universal to live s

2020-02-26 13:56发布

问题:

I have my website setup and working by modifying the universal-starter seed found here: https://github.com/angular/universal-starter and I have got my site working (rendering HTML) on the localhost node server, now I have a SSH connection and node installed, now I am here:

And I am not sure where I put my /dist files etc and then start the nodejs server.

NodeJS/npm installed shown here:

回答1:

This was actually easier than it seemed, I just needed to install pm2 with npm so I could make the nodejs process constant, then use RewriteEngine settings in the .htaccess file where I wanted to my angular universal application to render (it was in a subdomain folder) and ensure the port is correct and it linked perfectly and loads html in source allowing proper SEO and indexing



回答2:

@Cacoon I hope this is what you did.

This first part is an existing answer from m98 on this Github issue. Just posted here for clarity

  1. Build your app: npm run build:ssr
  2. Move the dist over to your server
  3. install PM2 npm install pm2 -g
  4. On your server, use PM2 to run the server bundled app pm2 start dist/server.js
  5. If you're using Nginx, or other web servers, make sure to redirect requests to the port that the app started with PM2 is listening on.

UPDATE (2/7/2020)

For Apache try this in the bottom of your httpd.conf file:

<IfModule mod_rewrite.c>
 RewriteEngine On
 # If an existing asset or directory is requested go to it as it is
 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
 RewriteRule ^ - [L]

 RewriteRule ^ /dist/browser/index.html
</IfModule>

<VirtualHost *:80>
 ServerName localhost
 ServerAlias localhost
 ProxyPreserveHost On
 ProxyRequests Off
 ProxyPass / http://localhost:4000/
 ProxyPassReverse / http://localhost:4000/
</VirtualHost>

<VirtualHost *:443>
 ServerName localhost
 ServerAlias localhost
 ProxyPreserveHost On
 ProxyRequests Off
 ProxyPass / https://localhost:4000/
 ProxyPassReverse / https://localhost:4000/
</VirtualHost>

You have to set the virtual host in the httpd.conf, and not in a .htaccess file. You can set Mod_rewrite at the same level of your index file in an .htaccess file, but you cannot set virtual host at that level.

Also, make sure you enable the modules for the proxy, like mod_proxy_express in your httpd.conf file

This is for developing locally. Change localhost to your domain when deploying to prod.