Here is a step-by-step tutorial on how to create a build with Vite and deploy it for testing on a local server using Nginx. This tutorial assumes you already have Node.js and npm installed on your machine.
Step 1: Create a new Vite application (skip this step if you already have one)
- Open the terminal and run the following command to create a new Vite application:
npm create vite my-vite-app
- Select the template you prefer (e.g., “react” or “vue”) and follow the instructions to create the project.
- Change to the project directory:
cd my-vite-app
Step 2: Develop and test locally
- Start the Vite development server:
npm run dev
This will start a development server at http://localhost:3000
. Open your browser and visit that URL to see your application in development.
Step 3: Build the application
- When you’re ready to build the application for production, stop the development server (press
Ctrl + C
in the terminal). - Run the following command to build the application:
npm run build
This command will generate a dist
folder in your project directory containing files optimized for production.
Now, we can perform two distinct steps:
- Create a development server.
- Create a production server.
Create a development server to test the built application
To deploy the test server, we will use serve. First, we need to install it:
npm install -g serve
This command will install the serve
tool globally, meaning you can access it from anywhere on your system.
After installing serve
globally, you can use it to serve your Vite application built in production. Here’s the process:
- Make sure you have built your Vite application:
npm run build
- Navigate to the
dist
directory:
cd dist
- Run the following command to start a simple HTTP server with
serve
:
serve
This will start a server at http://localhost:5000
(or a different port if 5000 is occupied).
- Open your browser and visit
http://localhost:5000
to see your Vite application deployed usingserve
.
This method is very useful for quick local testing and is also suitable for simpler production environments. However, if you plan on a more serious deployment, you may want to consider configuring a more robust web server like Nginx or Apache.
Set up a web server (Nginx)
- Make sure you have Nginx installed. If not, you can install it following the provided instructions earlier.
- Create a configuration file for your application in Nginx. You can use the following command to open a new file:
sudo nano /etc/nginx/sites-available/my-vite-app
Replace “my-vite-app” with the name you prefer.
- Add basic Nginx configuration:
server { listen 80; server_name localhost; location / { root /path/to/your/project/my-vite-app/dist; index index.html index.htm; try_files $uri $uri/ /index.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
Make sure to replace “/path/to/your/project/my-vite-app/dist” with the full path to the “dist” directory of your application.
- Save and close the file.
- Create a symbolic link in the
sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/my-vite-app /etc/nginx/sites-enabled/
- Restart Nginx to apply the changes:
sudo service nginx restart
Or on more modern systems:
sudo systemctl restart nginx
Test the deployed application
- Open your browser and visit
http://localhost
or your server’s IP address. - You should see your Vite application deployed using Nginx.
That’s it! You have created a build with Vite and deployed your application for local testing with Nginx. Note that these steps are basic and may need adjustments based on your specific needs and server configuration.