0

I just recently bought a Linux hosting server from godaddy. I don't know if i messed something or i am just being noob, i can't seem to test a site using IP XXX.XX.XX.XX. After researching for a bit i found that i can preview it using XXX.XX.XX.XX/~rootuser/

And since i am trying to run multiple sites from same server all sites are separated according to directory.

XXX.XX.XX.XX/~rootuser/site this loads a site.

All links in my php use a constant value of $_SERVER['SERVER_NAME'] as url. But all the links are shown as XXX.XX.XX.XX/script.js likewise in the dev tool.

I though about using Apache but couldn't find ant helpful topics. I also tried using hosts file but ended in failures.

I am really noob at this and really at my wits end. and really sry for using bad terminologies.

Is there a way test sites using ip without using /~rootuser/ after the ip or let php get XXX.XX.XX.XX/~rootuser/site as server name.

EDIT

Using IP Always shows this even if i delete hello.html http://prntscr.com/89wx25

chris85
  • 23,255
  • 7
  • 28
  • 45
hengecyche
  • 229
  • 2
  • 15
  • server_name is literally the dns name of the server machine. it (usually) has nothign to do with the name of the site being served. try `$_SERVER['HTTP_HOST']`, which is what the client requested by the http header `Host: ...` line. – Marc B Aug 28 '15 at 15:26

3 Answers3

1

You can use only the ip if you own a vps or a dedicated server because they have a dedicated ip only for your hosting.

If you bought a shared hosting on your ip are hosted a lot of website, so you can't do what do you want.

Maverick
  • 855
  • 8
  • 23
  • basically without a domain i can't use server to test stuff? – hengecyche Aug 28 '15 at 15:30
  • you can use XXX.XX.XX.XX/~rootuser/yourwebsite – Maverick Aug 28 '15 at 15:31
  • i knw that but every link on the site uses the main ip so can;t even load stylesheet and scripts – hengecyche Aug 28 '15 at 15:35
  • just buy a domain. it's super cheap relative to your hosting cost, like $10/year. then you could have your domain.com/site1 or whatever – Tyler Collins Aug 28 '15 at 15:40
  • use the url `http://XXX.XX.XX.XX/~rootuser/yourwebsite/css/style.css` for example – Maverick Aug 28 '15 at 15:41
  • If you only need to test, you can always create a host file entry (/etc/hosts on Linux, C:\Windows\System32\drivers\etc\hosts on Windows) for a fake domain, like "hengecyche-test.com" and setup the account with that fake domain. The apache vhost config will be expecting the host header and since you're using a host file, it'll be sent when you locally visit "hengecyche-test.com". – Brian Clifton Aug 28 '15 at 22:26
1

I will try to answer your two questions thoroughly...

a) "Is there a way test sites using ip without using /~rootuser/ after the ip[?]"

Independantly of GoDaddy, of course you can. The server (e.g., Apache) has to be configured to serve that website for that IP address. However, logically, you won't be able to serve more than one website per IP that way. That is not practical and that is one reason you'll want to use multiple names that map to that IP).

As for GoDaddy, it all depends what kind of package you "bought" and what it allows you to do. If you have a dedicated IP, maybe you can ask it to serve a particular website for that IP, but chance are you bought a shared hosting plan and therefore the same IP is used for multiple users / clients / sites. So they won't default that IP to your website. So I would suggest that you rely on a name in practice. The name will usually be a domain name you register, like EXAMPLE.COM.

Technically, if you had the control of the machine configuration (chance are not on GoDaddy, especially if you bought a shared hosting plan), you can even tell the machine to serve your website for "anynameyouchoose" and locally (i.e., on your own computer), you can make sure that it maps "anynameyouchoose" to the target host IP XXX.XX.XX.XX. In Linux and Windows, there is an hosts file where you can specify a mapping between a name and an IP. You can even decide to resolve Microsoft.com to XXX.XX.XX.XX if you want and if the machine located at XXX.XX.XX.XX is configured to serve a specific content for Microsoft.com, it will!

In practice, there is public name servers that map domain like Microsoft.com to the registered IP, but it is good to know how it works, and it might come handy when you want to fool a friends on April 1st. ;-)

That is a bit advanced, but if you understand what I just said, you may also try to associate any made up domain name in your host control panel to your site content and then map that name to the IP of the machine where your site is. It might work, but it all depends what GoDaddy or any host you'll use allow you to do.

I think GoDaddy currently offer a free domain registration with your hosting plan or you can at least buy one (I'm sure you plan to buy one or more anyway). Some host (I do not know for GoDaddy) also offer their users some generic sub-domains (your-site-name.godaddy.com -- where GoDaddy.com can be anything the host use as their base domain for their users). That could also give you a root host name, without the need to deal with a deep root path.

You can also buy only one domain name and create lot of subdomains to map to each of your sites if you have multiple sites. Like if you buy EXAMPLE.COM, you can then create SITE1.EXAMPLE.COM, SITE2.EXAMPLE.COM, etc.

That is all if you absolutely want to avoid to deal with a variable root path in your code.

b) "[...] or let php get XXX.XX.XX.XX/~rootuser/site as server name[?]"

If you want to do this, in PHP, you might want to set some configuration variables to hold your host (IP or name) and your root path (e.g., /~rootuser/site). So even if you run your site at multiple hosts with different constraints, you limit the amount of code you have to change to the configuration variables. You can try to overwrite the $_SERVER['SERVER_NAME'] value, but I would advice against doing it that way, because that is not the purpose of this special variable and that will create more problems down the road.

There are other parts of the URL that can change depending of the host like the protocol (http or https) and the port number. If you find out you have to repeat yourself too much in your code, create yourself variables or functions you can re-use.

If you want to manage this automatically, you will want to look into these indices of $_SERVER super global:

  • HTTP_HOST : this will give you the host IP or name as typed in the address bar of the browser.
  • REQUEST_URI : this will give you everything after the HOSTNAME (path + query string)

So for the address http://www.example.com/~root/site1/sub-directory/page?var1=abc&var2=def the values will be:

  • $_SERVER['HTTP_HOST'] => www.example.com
  • $_SERVER['REQUEST_URI'] => /~root/site1/sub-directory/page?var1=abc&var2=def

You can read about what is usually available in $_SERVER there: http://php.net/reserved.variables.server

Take note that some variables in $_SERVER might be only available for some server (Apache, IIS, etc.) But do not worry much about this for now. Most people tend to use Apache with PHP and the common server variables you will want to use should be available in all popular servers.

Of course if you use a sub-directory as in my example, you won't be able to easily figure out what is your "root path" (i.e., /~root/site1). So you'll have no choice than to manually configure it for each setup.

I also encourage you to take advantage of function phpinfo(). Create a page named info.php with this code:

<?php
phpinfo();

When you will visit that page, you will see everything that is available in $_SERVER, along with lot of other useful info. For example if you visit http://hostname/the/path/to/info.php?var1=abc&var2=def -- You will see in which variables the query string "var1=abc&var2=def" is included.

You may want to create a default configuration file (config.php) that optionally include a file that serve to "overwrite" the default value if needed (config.env.php):

config.php can be:

<?php
$config['host'] = $_SERVER['HTTP_HOST'];
$config['path'] = '/';

if (file_exists('config.env.php')) {
    require('config.env.php');
}
?>

config.env.php on your GoDaddy account can then be:

<?php
$config['path'] = '/rootuser/site1';
?>

This config.env.php file will be the one that change from your multiple setups of the same site. For example you can have a "development" version locally, a "test"/"staging" version online and a "production" version online. Each of those environment can have their own specific configuration.

You will then want to include config.php in all your pages so the configuration variables are available.

Also, in your pages, you can build all your internal links (link inside the same site) without specifying the protocol, host and port. Those links will therefore be "relative to the host". So if you link to "/~rootuser/site/sub-directory/page-name", you won't need to specify the "http://host:port", it will use the actual one. Why repeat yourself, add more characters, etc. when you don't have to? ;-)

I would also suggest that you always use absolute path (i.e., starting with /) for all your internal links instead of be tempted to put relative path. For example, use "/section-x/sub-y/page-z" instead of using "sub-y/page-z" in your page located at "http://host/section-x/index"). That will usually create yourself less headache to manage, unless you really have a good reason and know what you do.

Of course, if you can eliminate the need to have a variable root path in all your environments, that will simplify your code base.

I also want to mention that there exist an HTML tag that allow you to define the base path for all the links in a webpage. However, you will have to understand correctly how it works. I suggest you go check this if you are curious about it: Is it recommended to use the <base> html tag? -- this is not what I would recommend you to do, but it is good to know it exists.

You should also set yourself a local development environment if not already done (WAMP or LAMP packages can help you get a quick start; look for WampServer if you are on Windows).

I hope that is clear and that this make you understand the whole thing a bit better. Otherwise, feel free to ask questions so I can improve my answer.

Community
  • 1
  • 1
0

Because many domain names can point to the same IP address (literally, a nearly limitless number of domains could all point to the same IP), Apache, and other webservers have configurations to look up the name and find the right place to go to show the website.

In Apache, it's a configuration called a 'VirtualHost' block. Here's a simplified/sample one for a website I host myself.

<VirtualHost *:80>
    # we don't list a specific IP here - so it would accept a connection
    # from any the machine has. The ServerName is the important part here.
    ServerAdmin webmaster@example.net
    ServerName portal.example.net
    # If this was the only VirtualHost set, or it was setup first in the
    # configuration, it would catch all websites on this IP, unless
    # another Vhost was defined for a name. Ordering is important though.

    DocumentRoot /var/www/sites/portal.net/html/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/portal.error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/portal.access.log combined

</VirtualHost>

In this example, I go to a website URL: portal.example.net (not a real website, just an example), on the IP address that name points to, and the webserver sees the name given by the browser - it's in the Host: header, and looks up that block from the configuration, and then goe to the right place on the disc - here, /var/www/sites/portal.me/html/ to show the site.

If you run more than one website on a machine, you'll need something similar in Apache, Nginx or whatever website you want to see. If you are only running a single website, then you could just default everything to point the same place - but there are good reasons to do it in a better way anyway.

The Apache VirtualHost files need to be in the main Apache configuration - they tend to be in files named such as /etc/apache/site-enabled/200-websitename.conf. The numbers help to order the files specifically within the directory. There is also a directory /etc/apache/site-available/ where files could be placed, and then symlinked into the -enabled directory, if the sites would be occasionally disabled.

Alister Bulman
  • 31,832
  • 9
  • 65
  • 105
  • is this in .htaccess file on main apache .conf file? – hengecyche Aug 28 '15 at 15:57
  • Since it sounds like you're on a shared hosting account, you don't have access to this file :( You'd only have access if you have a VPS or another product where you have root access on the box – Brian Clifton Aug 28 '15 at 22:29