Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

perkola's avatar

Two sites on same server

Apologies if this is has already been covered, I've read similar posts but I just can't seem to understand how to get this working.

I have two different sites accessible via two GitHub repositories that I would like to deploy to the same server (Digital Ocean) via Forge.

I have created the domains domain1.com and domain2.com and they both point to the /public directory. When i SSL onto the server i have two folders with the same name in my home directory (as the forge user).

How would I go about to get these both sites working first with only ips and then with domain names. I've heard something like I should point both my domain names to the same IP but how will the server tell the difference? And how will I go about it if I can't access the domain names just yet, I only have the server IP from Digital Ocean at the moment.

Thanks!

0 likes
10 replies
elpete's avatar

You just need a different Nginx site for each domain you'd like to host. Laravel Forge makes this really easy if you are using it.

Your Nginx configuration could look something like this:

server {
    listen 80;
    server_name example.com;
    root /home/forge/example.com;

    # FORGE SSL (DO NOT REMOVE!)
    # ssl_certificate;
    # ssl_certificate_key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/elpete.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_param DB_HOST "localhost";
        fastcgi_param DB_PASS "5zOvon4dc7oZ5g7VC3hS";
        fastcgi_param DB_USER "forge";
        fastcgi_param DB_NAME "elpete";
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Again, Forge is the easiest. I highly recommend it. (That configuration above was generated by Forge.)

1 like
perkola's avatar

@spardicurus I have exactly two files like that in my /etc/nginx/sites-available directory, one for each of my sites. However I don't know how to access both sites from the servers IP address.

perkola's avatar

@bashy that works for one of the sites. I set the server_name the same as the servers IP address. But what about the other site? My server only has one IP address (provided by Digital Ocean). Do you recommend specifying another port or something?

bashy's avatar

Oh, you only have one IP assigned to the server?

In that case, you can't assign two sites to the same IP unless you use another port (pretty sure that would work). The IP acts as a domain so using the same one on both will return the first site (which is sorted by filename ASC).

Any reason you need to use the IP itself?

stefr's avatar

@bashy I'm trying to do the same thing. I have a couple of sites on one server. These are all test projects. No need to assign a domain name to them. I was trying to figure out a way how I could access these sites by only using the ip address. I used to work with DirectAdmin. Here I could use the server ip and the project name (DA username) like http://111.222.333.444/~project1

So on nginx I could do this by assigning a port to the site?

server {
    listen 81;
    server_name 111.222.333.444;
    root /home/forge/example1.com;

---
server {
    listen 82;
    server_name 111.222.333.444;
    root /home/forge/example2.com;

The code above doesn't work. Can you help me out?

stefr's avatar

@bashy I've already figured it out! I had to add a firewall rule on port 81 on the 'network' tab in Forge!

perkola's avatar

@bashy the Digital Ocean VPS only gives me one IP when i create a new droplet :/

In the future I will have two different domain names that point to the server but for now I'd like it to work without those domain names.

bashy's avatar

@stefr Yeah always check the ports are open :) it will tell you if you try to restart Nginx in SSH.

@perkola You can buy an additional IP but depends how you're using the IPs. If it's only you viewing them, you could create a custom DNS entry in your hosts file to view them?

  1. Use two IPs
  2. Put the sites into sub directories
  3. Use different ports
  4. Create custom DNS on your computer or use a service like xip.io
Ticked's avatar

Hi,

I used to have a DigitalOcen droplet on which using Laravel Forge I created a server.

Following the notes on Forge I deleted the "default" site to then created 3 sites.

So I had something like:

site-a.com site-b.com site-c.com

On my domains DNS control panel I changed the settings as follows:

site-a.com to point to my server ip 000.000.000.000 site-b.com to point to my server ip 000.000.000.000 site-c.com to point to my server ip 000.000.000.000

Back on Forge I edited the Nginx configuration file for each site as follows

site-a.com - listen 80 to 81 site-b.com - listen 80 to 82 site-c.com - listen 80 to 83

Finally using Forge I when to "Active Firewall Rules" for my server and added the following:

Name: HTTP, Port: 81 Name: HTTP, Port: 82 Name: HTTP, Port: 83

When visiting the different domains like: site-a.com or www.site-a.com, site-b.com or www.site-b.com I was able to see what was expected, the different website, so all was working just fine.

Yesterday I messed up the server so I decided to delete it and start all over again, form creating a new server. I have done it as above but this time no luck at all!...

When visiting the new server ip 000.000.000.000:81 or 000.000.000.000:82 I can see the different sites but when visiting the domains all I get is "404 not found, nginx/1.8.0"

Please if anyone can help me I'll appreciated, has Forge changed something recently I'm not aware of, the previous server where all was working fine was created just 5-6 weeks ago.

Thank you.

Please or to participate in this conversation.