Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy separated backend and frontend on same server

I have developed a project with Vuejs as the front-end and Laravel as the back-end api.In localhost they run in different ports.How should I deploy them in production?

like image 605
Shehan Rangana Avatar asked Feb 16 '19 13:02

Shehan Rangana


People also ask

Can I deploy frontend and backend separately?

A common application architecture is to host a front-end (user-interfacing) application and back-end (data-serving) application separately from each other. The goal is separation of concerns and therefore an increased ease in continuous integration between separate teams.

Can I host frontend and backend on same server?

You can't start both on the same one and if they are apart, it's impossible for the frontend code to access the backend endpoints. Fear not. If you really want to run both servers, you have to make sure that both are accessible through the same port.

Should frontend and backend be on separate servers?

front and back ends are should run on one server. You can run database on different server for the purpose of removing burden on the server. You don't have to use different servers for different components. It will work very efficiently even if you've only single combined server.

How do you integrate the front end and back end?

Learn about client-side and server-side rendering and create REST API endpoints to connect the front-end to the back-end. Get an introduction to the Model, View, Controller design pattern and create full-stack apps using MVC architecture. You've completed the Connecting Front-End to Back-End course!


1 Answers

When you build your Vue app for production, all the relevant files are in the dist folder. It does not run on any port, but instead the files are served by a webserver (e.g. Apache or Nginx). For a laravel api you normally have the public folder in the laravel installation visible, while having the rest of the files not directly accessible from the web.

I am going to assume that you want to deploy the api and the frontend on the same domain. The easiest way of getting this to work is by having a specific prefix for your api. In the case below, I use the prefix /api for all api requests, since this is the default for api routes. Anything else is considered a request for the frontend.

You set up your folder structure like the following. The public_html folder is what is normally loaded when going to example.com and you can test this by adding a test.txt file with some content and going to example.com/test.txt. The backend folder contains your laravel installation. The frontend folder contains everything that the dist folder contained after running npm run build.

/var
+-- /www
    +-- /vhosts
        +-- /example.com
            +-- public_html
        +-- /backend
        +-- /frontend

To get everything to work, we are going to remove the public_html folder and replace it with a symlink to backend/public.

cd /var/www/vhosts/example.com
rm -r public_html
ln -s ../backend/public public_html

When we check example.com, we now should see that we can make requests to the api using example.com/api/someroute. There are several ways we can make the frontend work with this, but for ease of deployment, lets create a symlink to the dist folder.

cd /var/www/vhosts/example.com/public_html
ln -s ../../frontend/dist app

If you are using hash mode for your Vue application and don't mind accessing the app through example.com/app, I believe this is all you would need to do. Otherwise you would need to modify the .htaccess file if you are using Apache, or change the rewrite configuration of whatever other webserver you are using. I imagine the .htaccess file would look something like this:

# We assume that the FollowSymLinks option is enabled in the main Apache configuration.
RewriteEngine On

# Rewrite anything that starts with `api` to the laravel index file
RewriteCond %{REQUEST_URI} ^/api
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Rewrite anything else to the frontend app
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /app/index.html [L]
like image 159
Sumurai8 Avatar answered Nov 15 '22 14:11

Sumurai8