How To Setup A Web Server And Host Website On Your Own Linux Computer

Welcome to small tutorial series on hosting a website on a Linux machine. This series of articles will teach how to setup a web server on a Linux computer and make it available online. The website we’ll host on our personal computer can be accessed from around the globe. In this article, we will install all the required tools to a setup web server. So let’s get started and setup web server.

The Linux distro we’ll be using for this setup is Ubuntu OS. However, this can be implemented in any Linux distro. At the end of this tutorial, you will be able to host your PHP and MySQL-based website on your own Linux machine.

Just an important note before moving forward. After following this tutorial, you will have set up your own web server. The real challenge, however, is protecting your server from thousands of active threats. If you’re not an expert in managing Linux server, it is strongly advised to use web hosting services that take care of security.

Before we continue, we’d like to tell you about Contabo. We host LinuxAndUbuntu on Contabo VPS. Check them out if you need web hosting. Prices start at $6.99 per month for an 8GB RAM and 4 Core CPU VPS.

contabo vps

The tutorial is divided into two parts. In the first part, we discuss the basic components and their installation part. In the next section, we shall write sample PHP code for a basic website and host it under the apache2 webserver.

Prerequisites To Setup Web Server

To set up a web server on your own Linux computer, we’ll require the following three components to be installed –

  • Apache2: apache2 is open-source HTTP server. It is still the most popular web-server used worldwide today.
  • php and php sqlite component: PHP is a server-side scripting language. PHP and its component will help you to interact with a backend mySQL database for your website.
  • mySQL: mySQL is a database solution in which you shall be storing your data in the table.

If you have installed the above components, you can skip this part and move to the next part here.

​How To Setup A Web Server

Install Apache2

Apache is open-source web-server software that powers much of the web today. It is maintained by apache-http-project. Explore more here.

Open your terminal and type in commands –

sudo apt-get update
sudo apt-get install apache2
setup web server apache running

To check if apache2 is installed properly –

sudo service apache2 restart

Open your web browser and open the link using ip–address of your server. If you are practicing locally, you can type in localhost or By default, Apache runs on port 80. You need not provide the port number in your browser. Or ip-address of your server. For example

It should show a message as it works! To change the port address, you need to edit the configuration file at /etc/apache2/ports.conf  and change the Listen 80 to your desired port number. After the edit, you need to restart the apache2 server.

edit apache2 port 80

Restart web server –

sudo service apache2 restart

Install mySQL

MySQL is the database management solution that helps you to store and retrieve data in tables. Since we shall be using PHP in this tutorial, we will also need to install the php5-mysql component.

sudo apt-get install mysql-server php5-mysql

To check if MySQL is installed properly, open MySQL on terminal with the command –

mysql -uroot

If you set the password during installation, open with -p parameter –

mysql -uroot -p
setup mysql for setup web server

Install PHP

PHP is an open-source web server scripting language. It is a back-end scripting language that will help you to interact with the MySQL database. For example, if you want to show the tabular employee list stored in your MySQL database on your website, with the help of PHP, you can interact with MySQL, retrieve the employee list and render it on the HTML page. The php5-mysql library helps you in this regard. PHP provides multiple auxiliary libraries for different needs. Php5-mysql is one among them, and we shall use that in our tutorial.

To search the available libraries.

apt-cache search php5-
search libraries php5

To install PHP and php5-mysql.

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
sudo apt-get install php5-sqlite
setup php5-sqlite website

To check if PHP is installed correctly, make file /var/www/html/info.php and add the following content to this file -​

create php file in /var/www/html

Restart apache2

sudo service apache2 restart

Open the web browser and navigate to If you are using a remote server, replace IP with the server’s IP address. Upon success, you should see the following webpage –

web server running successfully

Well, that’s it. You are ready with the basic setup required for this tutorial. In the next section, we code a sample webpage in PHP that would store and retrieve the information in the MySQL table. Then we host it under apache2. If you like this tutorial, share the tutorial with your friends and let them set up their own web server.

Continue reading to learn how to install WordPress on our own server.


Mohd Sohail is a web developer and a Linux sysAdmin. He also loves to write how-to articles, applications reviews and loves to use new Linux distributions.

Articles: 852


  1. Nice tutorial, but i’ve another problem, what if i shut down my machine? How my website will respond online if my machine is off. Please enlighten me.

  2. If I wanted to host a web page without putting it out on the world wide webs, supposing I have a router with DDNS address assigned and a vpn server running on said router, could I do this and then just give those I want to be able to access the site a login for the VPN and once on my network set things up such that they would then get directed to my website?

  3. Hi this is kind of of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience. Any help would be enormously appreciated!

    • Hi, Most people use WYSIWYG editors since it’s default in WordPress. Manually writing HTML is hectic and will cause problems if you don’t have preview feature.

Leave a Reply

Your email address will not be published. Required fields are marked *