如何在Ubuntu 17.10服务器上为hacks4liveprojects.com配置子域名?
Hey there! Let's walk through setting up your subdomain somethinghere.hacks4liveprojects.com on your Ubuntu 17.10 server step by step. I'll cover both DNS setup and web server configuration (assuming you're using Apache, the default on Ubuntu, plus a quick note for Nginx users).
First, you need to point the subdomain to your server's public IP address. This is done through your domain registrar's DNS management panel:
- Log in to your domain provider's dashboard (where you purchased
hacks4liveprojects.com). - Navigate to the DNS settings section.
- Add a new A Record:
- Hostname: Enter
somethinghere(this is the prefix for your subdomain) - Value: Paste your server's public IPv4 address
- TTL: Set to 300 (5 minutes) for faster propagation, you can increase it later if needed.
- Hostname: Enter
- Alternatively, you can use a CNAME Record pointing to
hacks4liveprojects.com, but A records are more straightforward for direct IP mapping.
Note: DNS changes can take anywhere from a few minutes to 24 hours to propagate globally, so give it some time before moving to the next steps.
Ubuntu 17.10 uses Apache's virtual host system to handle multiple domains and subdomains. Here's how to set it up:
Create a virtual host configuration file
Open a terminal on your server and run:sudo nano /etc/apache2/sites-available/somethinghere.hacks4liveprojects.com.confPaste the following content into the file. Since you want to use
/var/www/html/as the site directory, we'll set that as theDocumentRoot:<VirtualHost *:80> ServerName somethinghere.hacks4liveprojects.com ServerAlias www.somethinghere.hacks4liveprojects.com # Optional, if you want a www prefix for the subdomain DocumentRoot /var/www/html/ # Optional: Set up logging for easier debugging ErrorLog ${APACHE_LOG_DIR}/somethinghere-error.log CustomLog ${APACHE_LOG_DIR}/somethinghere-access.log combined </VirtualHost>Save and exit the editor (in nano, press
Ctrl+O, hitEnter, thenCtrl+X).Enable the new virtual host
Run this command to activate the site:sudo a2ensite somethinghere.hacks4liveprojects.com.confCheck for configuration syntax errors
Before restarting Apache, verify your setup has no mistakes:sudo apache2ctl configtestYou should see a
Syntax OKmessage if everything is correct.Restart Apache to apply changes
sudo systemctl restart apache2
If you're running Nginx instead of Apache, here's the quick setup:
Create a server block configuration file
sudo nano /etc/nginx/sites-available/somethinghere.hacks4liveprojects.comPaste this content:
server { listen 80; server_name somethinghere.hacks4liveprojects.com www.somethinghere.hacks4liveprojects.com; root /var/www/html/; index index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ =404; } # Optional logging error_log /var/log/nginx/somethinghere-error.log; access_log /var/log/nginx/somethinghere-access.log; }Save and exit the editor.
Enable the server block
Create a symlink to the sites-enabled directory:sudo ln -s /etc/nginx/sites-available/somethinghere.hacks4liveprojects.com /etc/nginx/sites-enabled/Test Nginx configuration
sudo nginx -tLook for a
test is successfulmessage to confirm no errors.Restart Nginx
sudo systemctl restart nginx
Once DNS has propagated, you can test in a few ways:
- Ping the subdomain: Run
ping somethinghere.hacks4liveprojects.comon your local machine. It should resolve to your server's public IP. - Visit in a browser: Go to
http://somethinghere.hacks4liveprojects.com— you should see the content from/var/www/html/just like your main domain.
Make sure your Ubuntu server's firewall allows HTTP traffic (port 80). If you're using UFW (the default firewall on Ubuntu 17.10), run:
sudo ufw allow 80/tcp sudo ufw reload
内容的提问来源于stack exchange,提问作者hacks4live

