如何搭建本地服务器?两台PC开机后怎样通过IP访问项目文件?
Hey there, let's tackle your two questions one by one—these are super common scenarios, so I’ve got you covered with step-by-step solutions!
Depending on your needs (temporary testing vs. long-term use), there are a few straightforward options:
方案1:Python内置临时服务器(快速上手)
If you just need a quick, no-fuss server for testing web pages or sharing files locally, Python’s built-in tool can’t be beat:
- Open your terminal/command prompt, then
cdto the folder you want to use as the server’s root directory. - Run the matching command for your Python version:
- Python 3.x:
python -m http.server 8000 - Python 2.x:
python -m SimpleHTTPServer 8000
- Python 3.x:
- Pop open your browser and visit
http://localhost:8000orhttp://your-local-ip:8000(replaceyour-local-ipwith your PC’s local IP address, like 192.168.1.100). You can swap the port number (8000) for something else—using port 80 will let you skip typing the port in the URL, but you’ll need admin/root privileges.
方案2:稳定Web服务器(Nginx/Apache)
For a long-term, production-ready server, go with Nginx or Apache. Here’s how to set up Nginx across different systems:
Windows
- Download the latest Nginx version, unzip it to a folder (like
C:\nginx). - Double-click
nginx.exeto start the server. Visithttp://localhostin your browser—if you see the Nginx welcome page, you’re good to go. - To point it to your project files, open
conf/nginx.conf, find the lineroot html;and replacehtmlwith your project folder path. Restart Nginx (runnginx -s reloadin the command prompt) to apply changes.
Linux (Ubuntu/Debian)
- Open your terminal and update your package list:
sudo apt update - Install Nginx:
sudo apt install nginx - Start the service:
sudo systemctl start nginx - Set it to launch on boot:
sudo systemctl enable nginx - The default root directory is
/var/www/html—drop your files there, or edit/etc/nginx/sites-available/defaultto change the root path.
macOS
- If you have Homebrew installed, run
brew install nginx - Start the service:
brew services start nginx - Visit
http://localhost:8080by default—you can tweak the port and root directory in the Nginx config file located at/usr/local/etc/nginx/nginx.conf.
You just need both PCs on the same local network (same WiFi/router), and a few simple settings to share files. Here’s how to do it across different OS combinations:
Windows ↔ Windows
- On the PC with your project files: Right-click the folder you want to share → Properties → Sharing tab → Advanced Sharing
- Check "Share this folder" and set a share name (e.g.,
MyProjects). - Click Permissions, add the user(s) you want to grant access to (you can use "Everyone" for quick access, but specific accounts are more secure), and give them "Read" or "Read/Write" permissions.
- On the second PC: Open File Explorer, type
\\[shared-pc-local-ip]\[share-name](e.g.,\\192.168.1.100\MyProjects) in the address bar, and hit Enter. - To make this auto-connect on boot: Right-click This PC → Map Network Drive, choose a drive letter, paste the share path, and check "Reconnect at sign-in".
Windows ↔ Linux/macOS
Linux (using Samba)
- Install Samba:
sudo apt install samba samba-common-bin(for Ubuntu/Debian; adjust for other distros) - Create a folder to share:
mkdir ~/MyProjects - Edit the Samba config file:
sudo nano /etc/samba/smb.conf - Add this at the end of the file (replace
your-usernamewith your actual Linux username):[MyProjects] path = /home/your-username/MyProjects read only = no browsable = yes guest ok = no - Set a Samba password for your user:
sudo smbpasswd -a your-username - Restart the Samba service:
sudo systemctl restart smbd - On Windows: Type
\\[linux-pc-local-ip]\MyProjectsin File Explorer, then log in with your Samba username and password.
macOS
- Open System Settings → General → Sharing
- Check "File Sharing", then click the "+" button to add the folder you want to share.
- Adjust user permissions (read/write or read-only) as needed.
- On Windows/Linux: Access the share using
smb://[mac-pc-local-ip]/[share-folder-name](on Linux, you can use a file manager like Nautilus to connect via SMB).
Critical Tips for Reliability
- Set a static local IP: To avoid the shared PC’s IP changing after reboot, either assign a static IP in your router’s admin panel, or manually set it in your PC’s network settings (Windows: Network & Sharing Center → Change adapter settings → Right-click your network → Properties → IPv4 settings; Linux/macOS: use system network settings).
- Check firewalls: If you can’t connect, make sure both PCs’ firewalls allow file sharing traffic (SMB uses ports 139 and 445; Samba uses the same). You may need to add an exception for these ports.
- Confirm same network: Double-check that both PCs are on the same subnet (e.g., both have IPs starting with 192.168.1.x).
内容的提问来源于stack exchange,提问作者Adrian William

