The term web server can be used to refer to both hardware and software, or both working together. For the purpose of this guide, we will focus on the software side and see how you can host a website on your Linux box.
A web server is a software program that receives and responds to client requests via the HTTP/HTTPS protocols. Its primary purpose is to display website content which, oftentimes, is in the form of text, images, and video.
A web server can either serve static or dynamic content. Static content, as the name infers, refers to content that hardly changes and is bound to remain the same. The server sends back content to the user’s browser as is.
Dynamic content is content that often changes or is constantly updated. To serve dynamic content, a web server must also work alongside a database server and server-side scripting languages.
This guide will demonstrate how to set up an Apache web server to host a website on your Linux system for free.
Table of Contents
Requirements
To follow along in this guide, ensure you have the following in place.
- A dedicated Public IP address can be obtained from your ISP.
- A Linux box, which can be a Linux server installation of your preferred OS variant. For this guide, we will use Debian 11.
Check out a few guides that can give you insights on installing a Linux server.
- How to Install a Debian 11 (Bullseye) Minimal Server
- How to Install a Debian 10 (Buster) Minimal Server
- How to Install Ubuntu 20.04 Server
- How to Install Rocky Linux 9 Server
- How to Install AlmaLinux 9 Server
- How to Install RHEL 9 for Free
You also required a LAMP server installed, which is an acronym for Linux, Apache, and MySQL (this can also be MariaDB). Here are a few guides on how to install the LAMP stack in Linux.
- How to Install LAMP on Debian 11 Server
- How to Install LAMP on Debian 10 Server
- How to Install LAMP Stack in Ubuntu 20.04
- How to Install LAMP on Rocky Linux 9/8
- How to Install LAMP Stack in AlmaLinux 9/8
- How to Install LAMP on RHEL 9/8
How to Host A Website on Linux Server
In this section, we will proceed and discuss the main components of a web server.
What is Apache?
Apache is a popular free and open-source cross-platform web server that is released under Apache License 2.0. It’s one of the most widely used web servers accounting for nearly 32.2% of the web server market share.
To check the latest version of Apache available, and if it is installed on your server, run the command:
# apt-cache policy apache2 (On Debian-based OS)
From the output, you can see the parameter Installed: (none) implying that it is not installed yet. You also get information about the latest version being offered by Debian / Ubuntu repository, which in this case is 2.4.52.
On modern Red Hat distributions, you can check for the availability of Apache using the following dnf command as follows.
# dnf search httpd
From the above output, you can see that the Apache httpd package is available for download. If Apache is not installed on your system, use the ‘apt‘ or ‘dnf‘ package managers to install Apache as shown.
On Debian-based systems:
$ sudo apt install apache2 -y $ sudo systemctl start apache2 $ sudo systemctl enable apache2 $ sudo systemctl status apache2
On Red-Hat-based systems:
# dnf install httpd -y # systemctl start httpd # systemctl enable httpd # systemctl status httpd
What is MariaDB?
A fork of MySQL, MariaDB is one of the most popular and open-source relational database management systems. Nowadays, it’s preferred to MySQL due to its faster speeds in replication and performing queries as well as security and a vast array of storage engines.
To install MariaDB, On Debian-based systems:
$ sudo apt install mariadb-server mariadb-client -y $ sudo systemctl start mariadb $ sudo systemctl enable mariadb $ sudo systemctl status mariadb
The following output shows that MariaDB is installed and running as expected.
To install MariaDB, On RHEL-based systems:
# dnf install mariadb-server -y # systemctl start mariadb # systemctl enable mariadb # systemctl status mariadb
What is PHP?
PHP is a recursive acronym for PHP Hypertext Preprocessor, which is a popular general-purpose scripting language that is mostly used in web development.
To install PHP, On Debian-based systems:
$ sudo apt update $ sudo apt upgrade $ sudo apt install ca-certificates apt-transport-https software-properties-common $ sudo add-apt-repository ppa:ondrej/php $ sudo apt update $ sudo apt install php8.0 libapache2-mod-php8.0
To install PHP, On RHEL-based systems, you need to first enable the EPEL repository.
$ sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm [RHEL 9] $ sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm [RHEL 8]
Next, enable the Remi repository, which offers the latest version of PHP on RHEL-based systems.
$ sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm [RHEL 8] $ sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm [RHEL 8]
Once EPEL and Remi repositories are enabled on the system, you can install PHP as shown.
# dnf module list php # dnf module enable php:remi-8.0 -y # dnf install php php-cli php-common
With all the components installed, you can now build your website using WordPress CMS, which is software that makes it easy or users to develop and manage a website without necessarily having knowledge of web design languages such as HTML, CSS, PHP, and Javascript.
Creating a Simple Website Using WordPress in Linux
For demonstration, we will install WordPress on Debian 11 and RHEL 9 systems, which will provide a sample website that can be further customized to your preference.
This section assumes that you already have the LAMP stack installed.
Step 1: Install Additional PHP Modules
To proceed, install additional PHP modules that are required by WordPress as shown.
To install PHP modules, On Debian-based systems:
$ sudo apt install php libapache2-mod-php php-pear php-cgi php-common php-mbstring php-zip php-net-socket php-gd php-mysql php-bcmath
To install PHP modules, On RHEL-based systems:
# dnf install php-gd php-soap php-intl php-mysqlnd php-pdo php-bcmath php-curl php-zip php-xmlrpc wget
Step 2: Create a Database for WordPress
Written in PHP, WordPress is a data-driven, free, and open-source content management system. A database is an essential component of WordPress.
The database is used to store all the blog posts, pages, categories, comments, themes, plugins as well as WordPress configuration files.
To create a database for WordPress, login to the MariaDB database server:
$ sudo mysql -u root -p
Next, create a database as shown
CREATE DATABASE wordpress_db;
Next, create a database user and assign all privileges to the user on the database.
GRANT ALL PRIVILEGES ON wordpress_db.* to wordpress_user@localhost identified by 'P@ssword321';
Then finally reload the grant tables to save the changes made and exit the database.
FLUSH PRIVILEGES; QUIT;
Step 3: Download WordPress
With the database in place, proceed and download the latest WordPress tarball file using the wget command.
$ wget https://wordpress.org/latest.tar.gz
Once downloaded, extract the compressed file using the tar command.
$ tar -xvzf latest.tar.gz
The command extracts the contents of the file into a folder called wordpress. Move or copy the folder into the Document Root for the Apache webserver.
$ sudo mv wordpress/ /var/www/html/
Next, assign the following permissions and ownership rights.
$ sudo chmod 755 -R /var/www/html/wordpress/
$ sudo chown -R www-data:www-data /var/www/html/wordpress/
Step 4: Create an Apache Virtual Host for WordPress
The terminology virtual host refers to the practice of hosting multiple websites on a single server. If you intend to host multiple websites on a single server, you need to create a virtual host for each website.
In this case, you need to create a Virtual host for the WordPress website as follows.
$ sudo nano /etc/apache2/sites-available/wordpress.conf [On Debian] # vi /etc/httpd/conf/httpd.conf [On RHEL]
Paste the following lines of code to define the virtual host. For the ServerName directive, provide the server’s IP address or Fully Qualified Domain Name, which should point to the dedicated public IP address.
<VirtualHost *:80> ServerAdmin admin@your_domain.com DocumentRoot /var/www/html/wordpress ServerName 192.168.0.100 <Directory /var/www/html/wordpress> Options FollowSymlinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combined </VirtualHost>
Save the changes and exit the file.
To connect to the database, some additional modifications are needed. So, navigate into the wordpress folder.
$ cd /var/www/html/wordpress/
Next, update the wp-config.php file with the contents of the wp-config-sample.php file.
$ cp wp-config-sample.php wp-config.php $ sudo nano wp-config.php
Next, update the database name, db username, and password directives with the database details.
Next, enable the new WordPress site as follows on Debian-based systems.
$ sudo ln -s /etc/apache2/sites-available/wordpress.conf /etc/apache2/sites-enabled/wordpress.conf $ sudo a2ensite wordpress $ sudo a2enmod rewrite $ sudo a2dissite 000-default
To effect the changes, restart Apache.
$ sudo systemctl restart apache2 [On Debian] # systemctl restart httpd [On RHEL]
Step 4: Complete WordPress Setup on a Browser
To complete the setup, browse your web server’s IP address as shown:
http://server-ip
You should get the WordPress welcome page displayed as shown. Select your preferred language and click ‘Continue’.
Next, fill in the site details.
Then click ‘Install WordPress’ to complete the WordPress setup.
If everything went right, you will get a confirmation that the installation was successful. To log in, click the ‘Log In‘ button.
This ushers you to the WordPress dashboard as you can see. At this point, you can experiment with various themes to enhance the appearance of your sample website.
Step 5: Access WordPress Using Port Forwarding
Since you are self-hosting your web server from a Linux system at home or your Local Area Network (LAN), the next step is to make it accessible to external users or users outside your LAN (Local Area Network). This is where port forwarding comes in.
Port forwarding, also referred to as port mapping, is a technique that allows external devices to access servers or resources within a private network over the internet. The whole idea is to access private networks from outside, without which it would be impossible since external devices cannot communicate with internal IP addresses.
In your setup, you need to forward the port on which the web server is listening, (in most cases, this is port 80 for HTTP traffic or 443 for HTTPS) as well the static private IP address of the web server.
So, log in to your router and head over to the Port forwarding section. In our example, we are using the DLink router to port forward the web server’s ports (80 and 443) and private IP (192.168.0.100) to the Dedicated IP Public IP assigned by the ISP.
In your case, specify the ports and private IP of the web server and save the changes.
To save the changes, you might be required to reboot the router. So, go ahead and do exactly that.
Once the port forwarding is properly carried out, you can now reach your web server outside your network via the Public IP address.
Conclusion
In this guide, we have demonstrated how you can self-host your web server using Apache on a Linux box. Your feedback on this guide is welcome.
Releasing the ports on the router is more challenging than it seems. I tried several tutorials and it didn’t work.
Using the CloudFlare tunnel, you will only need a domain and a free CloudFlare account. This was the best solution I found for hosting my sites locally.
@Mark,
Configuring routers can indeed be challenging, and it often involves dealing with firewall settings and port forwarding rules, which can vary depending on the router’s make and model.
If you need further assistance with that, I’d be happy to try and help
Or you simply use iLinux OS with 10 Servers preinstalled, with full GUI…
I was looking for such a guide for a long time, I am new to Linux and I wanted to learn how to create and host a website on my local linux system,
Thanks for creating such a helpful article, now I can able host a website locally on my Linux box using these instructions.
Nice article, How can I keep my website alive even when my pc is off. How can that happen?
How can we add a minimum amount of security? also, can we proceed with the above steps using a Linux installed inside a Virtualbox in windows?
Yes, I’m doing this with virtual box ubuntu 20.04. Only the virtual server settings are creating problems for me.
Assuming 100 concurrent web users at a time visiting my server, is it possible to handle if my web-server is a desktop with 2Gb ram and 4Gb GPU attached??
@Melaku,
Yes, only if you do proper optimization of web server and have CDN in place..
How proper optimization of web server(debian linux) is done and what is CDN?
@Melaku,
For Apache Tuning & Optimization, this article will help you – 5 Tips to Boost the Performance of Your Apache Web Server.
Use cloudflare CDN for faster loading of website.
Can i add a cluster of GPU (4Gb each) to increase the webserver performance and is that good approach? if there is a better solution as per performance and cost please tell me
@Melaku,
Yes, you can upgrade system hardware anytime for better Apache performance..
Hi, I already made a web server on my Kali Linux and also do a port forwarding on virtual server tab but my web page only access from the device which are connected with my home wifi or the same network on which server is connected but i want it to be access from world wide what can i do for that please help me thanks.
@Moeez,
Please have a public IP address setup on the server to make a web sever page accessible from outside world.
I got my public IP by searching on Google “what is my ip” but with that ip my webserver is not accessible from outside world if we try to access from outside world then it give error that web page at http://my public ip could not be loaded because “net::err_connection_timed_out. this error happen when i try to access my webserver from outside local internet from mobile what should i do now or from where i can get the public ip address
@Moeez,
You need to buy the Public IP address for your web server or else take hosting from cheap hosting provider they will provide you free IP address for your web server…
and how can i have public ip address setup please guide me i really need help
Great Article, Really Helpful…
Instead of manually installing webserver and stack manually, isn’t it better to host PHP site with platform like Cloudways, where the these packages are installed and configured automatically? This will save a lot of time, which you can instead spend on building your app.
so useful for me thanks …
Great Work… Thanks for this, Helped me a lot …
Thanks for the information, great article and tutorial
Hi mate, thanks for the great article, I followed your step of installing Drupal and after copying the extracted Drupal contents to /var/www, I wasn’t greeted with the page as you showed in the screenshot.
It’s the same Apache greeting page. So I tried to move those contents again into var/www/html directory again and still getting greeted with the same Apache default page. Could you tell me if I’m doing anything wrong? Thanks
@Bruce,
Delete the default apache index.html page and try again, I sure you will get drupal setup page as in this guide..
Um Yes! Great article! Would love further guides from you!
Planning to build my own web server too but i have problem. How do you connect your domain name to your webhost? Specifically how will i associate my webhost to a nameserver which later i can use those NS to my domain to.
@Mozgear,
That’s good to hear that you’re planning to setup your own webserver, for connecting domain to webhost, you need to create a DNA A Record on the domain register panel and point back to the IP address of Sever where actual web server files hosted..
Please do make a article on that..
please do create article on that topic would be helpful thanks..
good sir and thanks
it open website by ip address or something like local host ….
How to open website by typing eg ::: http://www.abc.com
@Mustaga,
You need to setup A recored for you domain in DNS and point to your server where Apache installed..
But if someone is using windows. Can that person still have Linux server been installed?
@Cypher,
Yes you can install Linux server there isn’t any major difference, except you must know about root, swap partition before installing..
I am using data card & I want host my website from my laptop so it can happen.
I am getting problem while host website in outside.
my website work only local host.
@Deepesh,
Yes you can host your website in your laptop, but you need a static IP address to make it available on the web..May I know what kind of problems you facing while setting up website?
I want to place hosting live for my company please help me with step so i can have some business and can join client too
@Rudra,
We’ve already presented the article with nice step-by-step article to host your own website under Linux server, now may I know what kind of help you looking for?
Thanks so much for this! Extremely helpful and easy to follow!
This guide was so helpful! I went the wordpress route instead, and everything works! Thank you so much!!
Hello superb explanation. I thank you and I find you have saved me a lot of time. Now my problem: I placed the drubal in /var/www and it does not load on my browser so I have to place it in /var/www/html for it to load. Is this a problem? Now it seems to complicate the installation somewhat. Thank you!
@Nj,
It’s depend on which Linux OS version you using, in CentOS Apache default path is /var/www/html and in Ubuntu its /var/www.
Why did you say that I shall never give IP address to someone you don’t know? How can other people visit my site then? I am confused.
How do I create the php file in my apache directory?
@Bert,
Simply using vi or nano editor to create a file with .php extension. For example, index.php would be your new php file.
Hello Sir,
I want to know if i host a website on my local ubuntu server 14.04 .how can i access this site externally !.What are the settings that need to be done for this?
1) it is not a good idea to do it host a website on local server.
2) If you understand the risk, you can simply forward the port. All you need is a Static IP.
You should be able to do it yourself by following the guide, if however you seek my help you may send me your ssh server
Hello,
Thank you very much for your reply. Is this a suitable way to create a website in order for large amounts of people get there? I mean, is this going to work for a site with audio streaming, embedded videos, text, images, etc, being visited by lots of users? If so, what hardware would you recommend for something like that? Thank you!
That goes way beyond the scope of this, which is a testing server. Of course Linux can do what you are asking, 70% of everything served over the Internet came from a Linux server, across Linux routers and switches to you. ;)
Hello I am trying to install MySQL and keep getting this error:
E: Unable to locate package mysql
I googled it and people said to do the command
sudo apt-get update
But still didn’t made it work.
Thank you.
I fear you haven’t installed mysql. have you?
what is the output of
$ whereis mysql
and if you are unable to install mysql let us know what distribution you are using and its version.
Thank you very much for your reply!
I did this in my console:
root@ns357128:~# whereis mysql
mysql: /usr/bin/mysql /etc/mysql /usr/bin/X11/mysql /usr/share/man/man1/mysql.1.gz
Then tried to install it like on your tutorial:
root@ns357128:~# apt-get install mysql mysql-server mysql-client
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package mysql
root@ns357128:~#
I installed apache and
http://your-ip-address
works, so I assume apache is installed.Then I tried to install PHP:
root@ns357128:~# apt-get install php5 libapache2-mod-auth-mysql php5-mysql
Reading package lists… Done
Building dependency tree
Reading state information… Done
libapache2-mod-auth-mysql is already the newest version.
php5 is already the newest version.
php5-mysql is already the newest version.
0 to upgrade, 0 to newly install, 0 to remove, and 42 not to upgrade.
root@ns357128:~#
So I assume PHP is installed, but I did the info.php and placed it in the /var/www/html folder, but just appears as a blank page so I assume I need mySQL installed?
I did better research so I also assume I need to install this: https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/ but I wanted confirmation first.
I am using kimsufi dedicated server OS Ubuntu Server 14.04 “Trusty Tahr” LTS (64bits)
I am sorry for all the questions but I am really new to this and really want to install a forum on my dedicated server!
Thank you so much for your patience.
send us a mail with your ssh login details or remote desktop login credentials and we will solve this for you.
Sir , i started to create a website after reading your article , but i’m successful in publishing my website upto my local network . but i want to make it live through out the web ( outside my network also ) . and i think i had static ip too , but i’m unable to make my site live for whole web . please help me
@Vishal,
Have you opened port 80 on iptables firewall? if not open and check from outside…
@ Ravi Saive , i did not know how to open port 80 in iptables . please help me sir
@Vishal,
Use the following command to open port 80 in iptables firewall
thank you .it is very useful tuturial for installing LAMP Server
Welcome تور استانبول
Which are the differences between ‘/var/www/html‘ and ‘/var/www‘?
If i have created an html file with php on it, to be able to execute the file and see it on the browser where should i put my file?
Apache’s working directory in some distro is /var/www and in some disto /var/www/html.
If you have html directory at location /var/www/, then it means your working directory is /var/www/html. Put you php file here. set up the correct file permission. and finally point your browser to 127.0.0.1/Php_filename.php.
Replace Php_filename with the name of your PHP file.
Hope it helps.
Hi Avishek,
It’s nice to have this tutorial. Need information in detail. How can we access this URL using www. Need info about that setup
Thanks & Regards
Pavan.kusuma
Dear that’s a complex thing called DNS.
Wait we will be coming with DNS working soon.
Thanks @ Ananth S P,
It was a great pleasure knowing this.
This was a great tutorial, thank you. This will help me to build my first web server and hos my websites.
Thanks @ dario, for the wonderful feedback. All the success!
Thanks for the tutorial, might want to make a note to restart the apache service after installing PHP, other wise the phpinfo() script will not work
Welcome and Thanks for the suggestion.
it is not working how install drupal when we put on browser localhost it shwon this Apache 2 Test Page drupal not running.
which i already extract file in var/www/
kindly advise.
Tell which Distro you are using.
is there any html directory under www directory?
Have you set correct permission?
Thankyou! sir for this wonderful article.
I am having a pet project call wifi broadcast system but i currently not managed to break the networking.
we want to broadcast above server with wifi router to other device.
Please guide further..!!
Welcome Deepak.
Great article, keep it up Bro. We won’t mind if u give us on more on virtual server creations, and securing of it.
Thanks @ Joseph for such a wonderful feedback.
we would be working on your suggestions, soon.
Hey Avishek, this is a really neat article targeting amateurs like me. I’m going to use this and set my server up (I’m done until getting LAMP up), now its Drupal time.
Hope its ok for me to get in touch with you if I need help.
Cheers!
Thanks Vinod fro this valueable feedback. Kudos…
we will be happy to help you. Moreover we are starting our services, and you can take our service at very reasonable cost.
Really nice article Avishek!!!
I am not a very experienced Linux person but trying to understand which of the installation process (apt-get Or yum Or another) someone should use and concepts behind those?
Are these distro dependent or what?
Have you already any article published onto this If Yes then please provide me the link Or if you have some good details then I will be really thankful for that.
Welcome @ Vishal,
Apt and Yum are package manager for Linux Distros. Some Uses Apt other uses Yum or other package Manager. Yup Package Manager id distro dependent.
Nice post Avishek. It’s really helpful. I’ve have working on moodle. Procedure is same.
Thanks and keep it up.
Vasu..
Welcome @ Vasu
thank you for giving information about how to install Drupal
thank you .it is very useful tuturial for installing LAMP Server
Welcome @ keihan
Thank you very much for this well written article. I am a regular ubuntu user but have never tried making it a server. Awaiting subsequent articles (and lessons) from you about webpage/server development
Welcome @ Nayan