Deploy your Astro Site to is*hosting
此内容尚不支持你的语言。
is*hosting is a hosting provider offering VPS and dedicated servers in 40+ locations that you can use to self-host a static or server-rendered (SSR) Astro site. A VPS is suitable when your Astro site uses SSR and requires a persistent server, database connections, background jobs, cron jobs, or API routes.
Prerequisites
Section titled “Prerequisites”To deploy your Astro site to an is*hosting VPS, you will need:
- An Astro project
- An is*hosting VPS running Ubuntu with SSH access and
rootorsudoprivileges - A domain name pointing to your VPS
- A Node.js version that meets Astro’s minimum requirements installed on your VPS
Project configuration
Section titled “Project configuration”Your Astro project can be deployed to an is*hosting VPS as a static site or as an on-demand rendered site.
Static site
Section titled “Static site”Your Astro project is a static site by default. You don’t need any extra Astro configuration to deploy a static site to an is*hosting VPS.
Adapter for on-demand rendering
Section titled “Adapter for on-demand rendering”To enable on-demand rendering in your Astro project, add the Node.js adapter with the following astro add command. This will install the adapter and configure it in standalone mode in your astro.config.mjs file in one step.
npx astro add nodepnpm astro add nodeyarn astro add nodeHow to deploy
Section titled “How to deploy”You can deploy to an is*hosting VPS by building your site on the server and serving it with a web server.
The following instructions use Nginx as the web server, and PM2 to keep the Node.js server of an on-demand rendered site running.
Set up your VPS
Section titled “Set up your VPS”-
Connect to your VPS using SSH and install both the web server and Git:
Terminal window apt updateapt install nginx gitThe server commands in this guide assume you are logged in as
root. If you use another account, run administrative commands withsudo. -
Clone your Astro project to your VPS, install its dependencies, and build your site:
Terminal window cd /var/wwwgit clone YOUR_REPOSITORY_URL astrocd astronpm installnpm run build
Deploy a static site
Section titled “Deploy a static site”-
Create an Nginx configuration file at
/etc/nginx/sites-available/astroto serve the generateddist/folder:/etc/nginx/sites-available/astro server {listen 80;server_name your.domain.com;root /var/www/astro/dist;index index.html;error_page 404 /404.html;location / {try_files $uri $uri/ $uri.html =404;}} -
Enable the Nginx configuration and reload Nginx:
Terminal window ln -s /etc/nginx/sites-available/astro /etc/nginx/sites-enabled/nginx -tsystemctl reload nginx
Deploy an on-demand rendered site
Section titled “Deploy an on-demand rendered site”-
Install PM2 and use it to start the Node.js server:
Terminal window npm install -g pm2HOST=127.0.0.1 PORT=4321 pm2 start dist/server/entry.mjs --name astro-app -
Configure PM2 to restart the Node.js server after a reboot:
Terminal window pm2 startup && pm2 save -
Create an Nginx configuration file at
/etc/nginx/sites-available/astroto forward requests to the Node.js server:/etc/nginx/sites-available/astro server {listen 80;server_name your.domain.com;location / {proxy_pass http://127.0.0.1:4321;proxy_http_version 1.1;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}} -
Enable the Nginx configuration and reload Nginx:
Terminal window ln -s /etc/nginx/sites-available/astro /etc/nginx/sites-enabled/nginx -tsystemctl reload nginx
Add HTTPS
Section titled “Add HTTPS”After your domain points to the VPS, you can use Certbot to configure HTTPS for either a static or SSR deployment. Make sure ports 80 and 443 are accessible.
-
Install Certbot and its Nginx plugin:
Terminal window apt install -y certbot python3-certbot-nginx -
Request a certificate for your domain:
Terminal window certbot --nginx -d your.domain.comReplace
your.domain.comwith your domain and follow the prompts. -
Test automatic certificate renewal:
Terminal window certbot renew --dry-run
Update your site
Section titled “Update your site”To deploy changes to your site, pull the latest changes, install dependencies, and rebuild your site:
cd /var/www/astrogit pullnpm installnpm run buildIf your site is on-demand rendered, restart the PM2 process for the changes to take effect:
pm2 restart astro-app