简体   繁体   中英

How to set a name for Apache server?

I created successfully a apache server but I dont want to connect to it by typing 192.168.0.102 , I want a normal url like www.google.com. How can I do that? I went to httpd.conf and found the ServerName line but setting it to something like www.mysite.com doesnt seem to work. I also tried to use my external ip( https://www.whatismyip.com ) as server name but it doesnt connect. It only works if I try to connect to 192.168.0.102 or localhost. How do i solve this? Thanks

There are three basic things you need to know.

Virtual name hosting

HTTP allows multiple websites to be hosted on the same IP address and port. The client uses the Host request header to tell the server which site it wants to get data for.

ServerName is used as part of this.

… but the client needs to know how to send a request to the server first.

DNS

When a client makes a request to a server, it uses the IP address of the server in order to allow it to be passed over the network (or networks) to it. It is the address.

IP addresses are sequence so of numbers, which aren't very friendly for humans to work with.

DNS translates friendly names (like www.example.com ) into IP addresses.

The client has to look up the name to find the IP address. It normally does this through the main DNS system, and in order to get your name linked to your IP address you will need to find a domain name registrar and pay them.

It is also possible to set up DNS at a local level on a private network, and on a computer-by-computer level using a hosts file.

Routing

The IP address of the server has to be routable from the computer the client is running on.

192.168.0.102 is a private address, accessibly only on the same LAN. To make it accessible to clients on the Internet you need to either:

  • Set up your router to use port forwarding and then use the Internet facing IP address of the router (which https://www.whatismyip.com tells you) or
  • Give your computer a public IP address and configure your router to route traffic to it (this generally isn't possible on consumer grade routers).

In short, you can't. 192.168.0.102 is not accessible from the Internet it is internal IP.

But you have some alternatives, like if you like to access your computer from a hostname you can use dynamic DNS servers.

Or you want to test your code on a spectacular domain, you can add 192.168.0.102 with a domain to your hosts file, then only you can use this domain with your local computer.

But, If you really want to serve some content to the Internet from your local computer you have to find a DNS server service (like cloudflare) to point your domain to your public Internet ip not to 192.168.0.102.

You configure the virtual host and set the server name to the domain name you want. After that, Apache will check the requests and will use that virtual host if a request was made for that domain name. In order for that to work, that domain should point to your IP address where the server is running.

If you want to test if the configuration works, edit your /etc/hosts file and add that domain name to 127.0.0.1. After that you will be able to access to that virtual host if you try to access to that domain name from your browser.

More info here : https://httpd.apache.org/docs/current/vhosts/name-based.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM