GithubHelp home page GithubHelp logo

rupachowrasia / node-hosting-with-apache Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 3 KB

Configuring Apache to Serve NodeJS Application

JavaScript 100.00%
node apache nodejs-hosting nodejs-apache nodejs-virtual-host

node-hosting-with-apache's Introduction

Configuring virtual host using apache and nodejs

  • Install the following:

    $ sudo a2enmod proxy
    $ sudo a2enmod proxy_http
    $ service apache2 restart
  • Navigate to /etc/apache2/sites-available/

    $ touch <YOUR DOMAIN NAME>.conf
  • Now add following into /etc/apache2/sites-available/.conf

    <VirtualHost *:80>
        ServerName <YOUR DOMAIN NAME>
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full
        <Proxy *>
            Require all granted
        </Proxy>
        <Location />
            ProxyPass http://localhost:<YOUR PORT>/
            ProxyPassReverse http://127.0.0.1:<YOUR PORT>
        </Location>
    </VirtualHost>
  • Enable the site

    $ sudo a2ensite <YOUR DOMAIN NAME>.conf
  • Add domain to host

    $ sudo gedit /etc/hosts
    127.0.0.1   <YOUR DOMAIN NAME>
  • Restart apache server

    $ sudo service apache2 reload
  • Navigate to your nodejs app

    $ cd ~/path/to/your/nodeapp/app.js
  • Add following in the app.js file:

     var http = require('http');
     http.createServer(function (request, response) {
        response.writeHead(200, {'Content-Type': 'text/plain'});
        response.end('Node.js is running from site1.\n');
     }).listen(8080);
     console.log('Server running at http://127.0.0.1:8080/');
  • Goto the terminal, and type

    $ cd ~/path/to/your/nodeapp/
    $ node app.js
  • In browser type

     <YOUR DOMAIN NAME>

That's it, enjoy!

node-hosting-with-apache's People

Contributors

rupachowrasia avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-hosting-with-apache's Issues

test

Configuring virtual host using apache and nodejs

Couldn't host nodejs app with apache

I have created a small webapp using node/express & I want to host it with apache in Ubuntu. I did it using pm2(process manager for node js)
I have installed below :
- node js (version 6.11.4) ( and npm has version 3.10.10)
- pm2 (version 2.7.2)
- Apache ( version 2.4.18)

Below is the configured virtualhost for it :
( path : /etc/apache2/sites-available/adduser.com.conf)

<VirtualHost *:80>
ServerName adduser.com
ServerAlias www.adduser.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/EastAddUser

<Directory  /var/www/html/EastAddUser>
	Require local
	AllowOverride All
       	Options Indexes FollowSymLinks
</Directory>
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
	Require all granted
</Proxy>
<Location /East>
	ProxyPass http://localhost:8081  
	ProxyPassReverse http://localhost:8081
</Location>
ErrorLog /var/www/html/EastAddUser/error.log
CustomLog /var/www/html/EastAddUser/access.log combined

Kept node js application content at path : /var/www/html/EastAddUser

Below is the content of app.js file :
(Path : /var/www/html/EastAddUser/app.js)
var express = require('express');
var app = express();
var bodyParser=require('body-parser');
app.use(bodyParser.json())
const fs=require("fs");
app.use(express.static(__dirname));
var xmlOperator = require('./js/xmlOperation.js');
var server = app.listen(8081, function () {
console.log('Node server is running..');
});
app.get('/',function(req,res){
var path=__dirname+'/addUser.html';
res.sendFile(path);
})
app.post('/writeToFile', function(req, res) {
// function writes data in a file
});

Below is the content of package.json file :
(path: /var/www/html/EastAddUser/package.json )
{
"name": "eastadduser",
"version": "1.0.0",
"description": "eastadduser",
"main": "app.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start":"node app.js"
},
"author": "sayali",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"bootstrap": "^3.3.7",
"express": "^4.16.2",
"jquery": "^3.2.1",
"xml2js": "^0.4.19",
"xmlbuilder": "^9.0.4"
}
}

And html file is kept at path /var/www/html/EastAddUser/addUser.html
I have enabled the modules proxy & proxy_http, enabled this site using a2ensite command, started the app.js using pm2 (command : pm2 start app.js)

In browser, when I request to "www.adduser.com/East" , I get error as "The requested URL /East was not found on this server."

when I request for "localhost/East", then the webpage appears but it doesn't calls nodejs functions ( gets error : http://localhost/writeToFile 404 (Not Found) )

Where I did the mistake??

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.