Access Laravel Apps on Digitalocean using IP Address


How to access Laravel Project through IP Address on Digitalocean Droplet

Nginx server block file is where it comes to play around to define how you will access your Laravel, PHP Project, whether using IP Address or Domain name.

All the configuration we have to make is in in the Server Block File, let's edit the default server block file with the following command. 

sudo nano /etc/nginx/sites-available/default

Carefully scroll down to the section which define index names, In the line with all of the index names, we will add index.php to the list of allowed file types to deliver by default. What this line tells Nginx is to first look for an index file, then look for an index.php file, then an index.html file and so forth and send the matching file to the user. Nginx process the Index from top to bottom in the line, so the order is very important here, Index.php file must be processed prior to index.html file.

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

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;
}

Next thing we will define a server name, for this you can either write your droplet IP address or a domain name, depending on how you or your user want to access your site.

Since the topic is about how to access your website using IP Address:

simply copy past your droplet IP address in the server_name line as in the example bellow.For demonstration i am using a sample IP address as 100.100.100.100, you must replace it with your actual IP address.

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

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name 100.100.100.100;

    location / {
        try_files $uri $uri/ =404;
    }
}

Next define the path for your Laravel Project folder where it resides on the server. You have to add the path in the line that follows root in the server block file.
I know we don't have a project folder yet on the server, however we will just define one for now and use the same project folder name in the next step of our deployment process.

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

    root /var/www/example/public;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name 100.100.100.100;

    location / {
        try_files $uri $uri/ =404;
    }
}

Please note that our project root path is pointing to public folder followed by our project name, because the index.php file that reside in public folder is the entry point for all requests entering your application and configures auto-loading. 

The index.php file is also responsible for handling all the query parameters and passing it to laravel. So we need to do an adjustment to the very first location block in the server block file as bellow:

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

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

    server_name 100.100.100.100;

    # do the changes bellow, this is the first location block.
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # more location blocks continue below
    # (no changes needed beyond this point)
}

Next we need to do the following adjustment to the second and third location blocks, by removing the comments in the second location block, we tell the Nginx to use the php-fpm that we installed earlier. Please make sure to rewrite appropriate PHP version matching to your server, at the time of writing this article, it was php version 7.2 in my server.

Image title

If you see carefully the above picture, you should note that I have also removed the comments in the third location block, so I ask Nginx to ignore the .htaccess files which is related to Apache Server and not Nginx Server.

finally with every changes in place, your server block files must be similar to the following:

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

    root /var/www/example/public;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name 100.100.100.100;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

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

That is all for now and we have completed the configuration with Nginx, Let's save the file and reload the new configuration. To save it remember to press Ctrl + X and then type Y and then press enter.

Now that we have saved the file, make sure it is error free by typing:

sudo nginx -t

If everything was correct then you should get this notice in your terminal:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

This means you have no errors. Let's restart Nginx Server so that our changes can take affect.

sudo systemctl reload nginx

with this setup, now your server is ready to host the Laravel Project. Let's go ahead and deploy the Project from your development machine. 

Deploy the New Laravel Project

Create the folder in /var/www directory for your new project, make sure the project folder name matches the exact name that you defined in the line for root path in your New Server Block file which you created in the last step:

root /var/www/example/public;

I am not going to repeat the deployment process again, rest of the work is straight forward as I explained in my other article, Click here to read my other post on this topic if you are still not clear on how to proceed further.

If you have any other questions, experience or insights on "Access Laravel Apps on Digitalocean using IP Address" please feel free to leave your thoughts in the comments bellow, Don't forget to share the posts.
Written by Akram Wahid 1 year ago

are you looking for a chief cook who can well craft laravel and vuejs, to make some awsome butterscotch,
yes then it is right time for you to look at my profile.

Do you want to write Response or Comment?

You must be a member of techalyst to proceed!

Continue with your Email ? Sign up / log in

Responses

Be the first one to write a response :(

{{ item.member.name }} - {{ item.created_at_human_readable }}

{{ reply.member.name }} - {{ reply.created_at_human_readable }}