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

jpeterson579's avatar

Help set up Nginx subdomain that has laravel

Hi everyone, Im trying to set up a subdomain on Nginx that points to a folder that has all my 2nd laravel sites files. I already have a working version of laravel on this same server just as the main domain though.

UPDATED CODE file structure is:

var/www/laravel/public (Main Site laravel install working!)

var/www/api/public (Second laravel site want to point subdomain to)

I already followed a ton of tutorials with no luck. in etc/nginx/sites-available I made a copy of the working default that point to main site. Changed file name to api and content to:

server {
    listen   80;

    root /var/www/api/public/;
    index index.php index.html index.htm;

    server_name api.mysite.me;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

Then in terminal: sudo ln -s /etc/nginx/sites-available/api /etc/nginx/sites-enabled/api sudo service nginx restart

etc/hosts:

 000.0.0.0  localhost
 000.0.0.0  password password
api.mysite.me   localhost

 # The following lines are desirable for IPv6 capable hosts
 ::1     ip6-localhost ip6-loopback
 fe00::0 ip6-localnet
 ff00::0 ip6-mcastprefix
 ff02::1 ip6-allnodes
 ff02::2 ip6-allrouters

What am i doing wrong? I created the domain names for DNS, do I also need to do any kind of pointing of @ or Cnames?

0 likes
21 replies
noeldiaz's avatar

Maybe the error is because since you made a copy not the two of them have "default_server" in their definitions? I believe you can only have one set as "default". Remove that from the top two lines and try a restart again. Maybe that does it.

Also, if you want to test the syntax of your Nginx config files before you apply them run this command:

sudo nginx -t

That will test the files and tell you if any errors are found.

1 like
noeldiaz's avatar

Maybe then try removing the second line for ipv6? Are you using that? Since you are labeling it "localhost" I take it this is a test machine or maybe a VM? You should be fine without that entry so you could safely take it off.

Just looked at my config and I don't have that on my production server, but we are not using ipv6 here yet.

Edit: You made me curious and I found this:

http://serverfault.com/questions/277653/nginx-name-based-virtual-hosts-on-ipv6

Seems you just need to edit the second ipv6 like a bit and remove the tail end of it. Remove the "ipv6only=on" part.

jpeterson579's avatar

Actually this is on digitalocean. Using localhost because that is what was on default and default file is working for my main domain.

removing ipv6 got the nginx restart to go through. However now visiting my subdomain, api.mysite.com get site cannot be found. Error code: ERR_NAME_NOT_RESOLVED

noeldiaz's avatar

Ah, that could be part of the problem. You should need to give them proper names after registering your DNS subdomains. I bet the one with localhost worked since it was your only site and it was flagged as default. As soon as you add a second I believe you need to go back and give them both unique names on the "server_name" directive that match your DNS.

So rename the first one with you main site's dns, and the second one with your api dns name.

jpeterson579's avatar

No luck. Seriously i greatly appreciate the help! No idea why this is not working. I had these same setting on another site that was working, the only difference was i was not trying to run 2 copies of laravl. The subdomain was just html.

noeldiaz's avatar

That is bizarre. Your definition is identical to mine. Looking at it right now on an SSH window. An idea, have you looked in the /var/log/nginx/error.log file? Maybe the is more information there of what is going on?

I mean, here is the top of two of my sites:

server {
  listen 80 default_server;
  server_name server1.com;
  ...

server {
  listen 80;
  server_name server2.com;
  ...

The rest is identical to what you have.

noeldiaz's avatar

Are you getting a different error now? Because the one you originally mentioned did have to do with the ipv6 definition. here is another discussion about it, this time with some samples of the problem and solution:

http://stackoverflow.com/questions/17241554/unknown-error-nginx

What do your two site files look like now after you tweaked them (the ipv6 change and the server_name change)? And what error does it give you now on test/restart?

alenabdula's avatar

I just followed these steps on my local development machine and it worked for me.

  • Install Nginx
  • Create example config in the /etc/nginx/sites-available/
  • Add server block (see below)
  • System link the file to /etc/nginx/sites-enabled by running sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example
  • Open up /etc/hosts and add api.example.dev localhost
  • Create folders if necessary, sudo mkdir -p /var/www/example and sudo mkdir -p /var/www/example/public/, also create the index.html, nginx restart will fail if folders and files don't exist.
  • Restart Nginx sudo service nginx restart

Server Block

server {
    listen 80;
    listen [::]:80;
    server_name api.example.dev;
    root /var/www/example/public;
    index index.html;
    location / {
        try_files $uri $uri/ =404;
    }
}
jpeterson579's avatar

@alenabdula I think its a laravel issue because it can get s subdomain working like this but not if its running laravel...

jpeterson579's avatar

Instead of just adding the urls to DNS on digitalocean, maybe I need to do something with cname? or pointing * to @? I have no clue...

alenabdula's avatar

I don't think it's Laravel issue. Just add the PHP definition to pass the process to PHP-FM

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        #   fastcgi_pass 127.0.0.1:9000;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
bashy's avatar

Make sure that location for PHP files has a try_files $uri =404; and maybe fastcgi_split_path_info ^(.+\.php)(/.+)$;

1 like
alenabdula's avatar

I have verified that this is working on my development machine. The include snippets/fastcgi-php.conf; line includes fastcgi_split_path_info ^(.+\.php)(/.+)$; that @bashy mentioned see bellow. Although try_files $uri =404; didn't work for me instead try_files $uri $uri/ /index.php$is_args$args; did. As any additional routes were not being recognized.

Here's working config. Hope that helps.

-Alen

server {
    listen 80;
    listen [::]:80;

    root /var/www/api.laravel.dev/public;
    index index.html index.htm index.php;
    server_name api.laravel.dev;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}
alenabdula's avatar

Instead of just adding the urls to DNS on digitalocean, maybe I need to do something with cname? or pointing * to @? I have no clue...

Just saw this. You have to set cname at Digital Ocean.

CNAME api example.dev.

bashy's avatar

@alenabdula The PHP check wasn't for location / { } it's for PHP files to check they're real and to stop an exploit with /index.php/uploads/avatar/1.jpg being executed over PHP.

1 like
jpeterson579's avatar
jpeterson579
OP
Best Answer
Level 7

Ok so I believe I have an active subdomain however now the browser is returning an error when it loads. "Error in exception handler."

nginx/sites-available/default

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /var/www/laravel/public;
index index.php index.html index.htm;

server_name 000.000.00.0;

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

location ~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
}

nginx/sites-available/api

 server {
    listen 80;
    listen [::]:80;

    root /var/www/api/public;
    index index.php index.html index.htm;

    server_name api.mysite.me;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Any ideas?

jpeterson579's avatar

Solved FINALLY!!!!! So the above code works... Then just had to install laravel fresh and then run

chgrp -R www-data /var/www/api
chmod -R 775 /var/www/api/app/storage

I hope this long thread helps out someone else!

2 likes
jpeterson579's avatar

@alenabdula

I did not have to set a cname in my digitalocean DNS admin... I just had to set up dns for api.mysite.com

lwilliamsjr's avatar

Thanks this thread was helpful @jpeterson579 . I actually have the exact same config but my site is still mirroring the main site. That leaves DNS. How did you set yours up (details please)? @bashy . I think I got it working but I have to wait for DNS lag maybe? I'm posting these two articles incase they help someone: https://www.digitalocean.com/community/questions/how-to-setup-subdomain-on-nginx-server & https://www.digitalocean.com/community/tutorials/how-to-set-up-a-host-name-with-digitalocean

Please or to participate in this conversation.