Run npm dev on EC2
November 16, 2024 ยท AI Conversations
### Option 1: Use `pm2` to run the development server in the background
1. **Install pm2 globally on the server**:
sudo npm install pm2 -g
2. **Navigate to your Laravel project directory**:
cd /path/to/your/laravel-project
3. **Start `npm run dev` using pm2**:
pm2 start "npm run dev" --name laravel-vue-dev
4. **Save the pm2 process list** to ensure it will restart after a server reboot:
pm2 save
5. **Configure pm2 to restart automatically** on system startup:
pm2 startup
Follow the instructions that `pm2` provides to set it up correctly (it will give you a command to run depending on your system, e.g., `sudo pm2 startup ubuntu`).
6. **Reboot your EC2 instance** to ensure everything starts automatically:
sudo reboot
After this, `npm run dev` will automatically start when the EC2 instance reboots.
### Option 2: Using `systemd` to run `npm run dev` on startup
1. **Create a systemd service file**:
Create a new service file to run `npm run dev` as a background service.
sudo nano /etc/systemd/system/npm-dev.service
2. **Add the following contents to the service file**:
Replace `/path/to/your/laravel-project` with the actual path to your project.
[Unit]
Description=Laravel Vue Dev Server
After=network.target
[Service]
WorkingDirectory=/path/to/your/laravel-project
ExecStart=/usr/bin/npm run dev
Restart=always
User=ubuntu
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=development
[Install]
WantedBy=multi-user.target
3. **Reload systemd to recognize the new service**:
sudo systemctl daemon-reload
4. **Start the service**:
sudo systemctl start npm-dev
5. **Enable the service to start on boot**:
sudo systemctl enable npm-dev
6. **Verify that the service is running**:
sudo systemctl status npm-dev
This will automatically run `npm run dev` on boot, and it will restart the process if it stops for any reason.
### Notes:
– Option 1 (pm2) is more flexible and provides process management tools like monitoring, log viewing, and easier restarts.
– Option 2 (systemd) is a more native approach for Linux systems and is great for simple process management.
Choose the option that best suits your needs!