Ubuntu16.04部署ReactJS应用报错:无法复制剪贴板且端口5000不可访问
Hey there, let's tackle your React deployment problems one by one. You've got two main issues here: the clipboard error and the unreachable port 5000. Here's how to fix both:
1. Resolve the "Cannot copy to clipboard" Error
The error pops up because your Ubuntu server doesn't have a desktop environment (no X11 display), but the serve command tries to copy the app URL to the clipboard by default. To disable this behavior, just add the --no-clipboard flag when starting the server:
serve -s build --no-clipboard
This will stop the clipboard-related error from showing up entirely.
2. Fix the Unreachable Port 5000
Even though the terminal says the server is running, you can't access it due to one of these common issues:
a. Firewall Blocking the Port
Ubuntu 16.04 uses ufw as the default firewall. You need to allow incoming traffic on port 5000:
sudo ufw allow 5000 # Verify the rule is active sudo ufw status
Look for a line confirming 5000/tcp is allowed in the output.
b. Server is Bound Only to Localhost
By default, serve might only listen on localhost (127.0.0.1), which means it's only accessible from the server itself, not from external devices. To make it listen on all network interfaces, specify the bind address as 0.0.0.0:
serve -s build --no-clipboard -l 0.0.0.0:5000
Now you should be able to access the app via http://server_IP:5000.
c. Port 5000 is Already in Use
Check if another process is using port 5000 with these commands:
# Using netstat sudo netstat -tulpn | grep 5000 # Or using lsof (install it first if needed: sudo apt install lsof) sudo lsof -i :5000
If you see a process listed, you can either kill it (replace <PID> with the process ID shown):
sudo kill -9 <PID>
Or start serve on a different port, like 3001:
serve -s build --no-clipboard -l 0.0.0.0:3001
Bonus: Production-Grade Deployment Tip
While serve works for testing, it's not designed for production use. For a more stable, performant setup, use Nginx:
- Install Nginx:
sudo apt update && sudo apt install nginx - Copy your
buildfolder to Nginx's web root directory:sudo cp -r build/* /var/www/html/ - Restart Nginx to apply the changes:
sudo systemctl restart nginx
Your app will now be accessible via the default HTTP port 80, no need for the serve command anymore.
内容的提问来源于stack exchange,提问作者Dave

