Three simple ways to start a local webserver
When you first start out with web development you’re probably opening html files right in your browser. You’re probably using relative urls like /styles/style.css and this is working fine for you. Right up until you’re trying to load some files from a remote location and nothing really works anymore. You ask questions online and people tell you “oh just launch a local webserver and use it to access your files”. Yeah, easier said that done, right? In this post I will explain three ways to quickly and simply start a local webserver. I’m going to assume you’re either using OS X or a version of Linux for this tutorial.
When you’re using a Unix based system like Linux or OS X your machine should already have Python installed. Python comes with the ability to start a webserver for you, all you need to know is where your website files are and how to navigate to the in the command line.
Open up your command line and navigate to your website folder. For example: cd ~/Projects/my-website . Now that we are in the website’s main folder we can launch the http server:
python -m SimpleHTTPServer
That’s it, you should see some output in your terminal like:
Serving HTTP on 0.0.0.0 port 8000
Node.js and http-server
First, make sure that you have Node.js installed. If you don’t have it installed, go to the Node.js website and follow the instructions there. When you have Node.js set up it’s time to install http-server. Open up a terminal window and type:
npm install -g http-server
If this command throws an error at you, try to run it as sudo (sudo npm install -g http-server). When the installation is complete you can navigate to your website folder, for example type cd ~/Projects/my-website if that’s where your website is located. To start the server you should type the following command:
That’s all, you should see output like this:
Starting up http-server, serving ./ on: http://0.0.0.0:8080 Hit CTRL-C to stop the server
You now have a local server running at http://localhost:8080.
Using a LAMP installer
Many webservers in the wild use the LAMP stack. LAMP is short for Linux, Apache, MySQL, PHP. Apache is a webserver, MySQL is a type of SQL database and PHP is a server-side language. If you’re not comfortable with the command line or plan to do PHP development I recommend to install this stack on your development machine. A good installer for this is XAMPP. When you have XAMPP installed you get a graphical interface that you can use to start / stop your webserver and database server. XAMPP will also provide you with a folder that it uses to serve your files. So if you want to use this webserver for your project you will have to move that project in to the XAMPP web folder. Your webserver can be accessed through http://localhost .
If you feel like I forgot a method or maybe I forgot to mention something important, send me a tweet.