#Apache

#Vue

#Nuxt

#pm2

Deploying a Nuxt.js 3 App with Apache

2 years ago

149

The Deployment-Process for an Nuxt-App with Apache isn't really well documented. So here are some things I figured out while trying.

Run Nuxt-App with pm2

So we've successfully built our App with npm run build and now we wont to run it. So at first, we need to copy the Files from ./.output/ to our Server - for convenience we'll copy the Files to /var/www/%DOMAIN_NAME%/.


According to the Nuxt Documentation, we can now run the App with node ./server/index.mjs. We can use pm2 start "node ./server/index.mjs" to run it in a background Deamon and even as a multi-processed Cluster-Mode.


We could now stop here and just route port 80 on the Router to the default port 3000 on the Server. But if you want SSL (Nuxt supports an SSL-Mode, but it's not recommended for Production) or host multiple sites on your Server you have to configure a Proxy through e.g. Apache or Nginx.



Apache Configuration

As usual, the VHost-File for HTTP redirects to SSL. In the VHost-File for the SSL-Port, add the following to Proxy to the Node-Server.

Note: I keep the DocumentRoot to indicate where the Server-Files are, but it has no effect.


<IfModule mod_ssl.c>
  AddType text/javascript js mjs
<VirtualHost *:443>
    ServerName %DOMAIN_NAME%
    DocumentRoot /var/www/%DOMAIN_NAME%
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine On
    SSLProxyEngine On

    ProxyPreserveHost On
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/%DOMAIN_NAME%/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/%DOMAIN_NAME%/privkey.pem
</VirtualHost>
</IfModule>

To make the Proxy work, we also have to add/enable a few Apache-Mods:


sudo a2enmod proxy_http
sudo a2enmod ssl
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http

Run Certbot if necessary, restart Apache and that's it :)