Windows: How to Host Your Own Website Using Your PC as a WAMP Server
First, let’s try hosting a website using your personal computer with the Windows operating system. Less than
one-third of all websites use Windows,
meaning your hosting options tend to be a little more limited than
those of Linux developers. But if ASP.NET and C are what you code in,
then Windows is the way to go.
Step 1: Getting Started With WAMP
To make this super easy, we’ll use a WAMP installation program (of which there are
several) called
WampServer.
This will cover your Windows, Apache, MySQL, and PHP. You could also
opt to install each package manually, but this process requires much
more work and is error-prone.
WampServer is one of several environments available to create Apache, MySQL, and PHP applications on Windows.
First, download the 32-bit or 64-bit WAMP-binary
here,
follow the on-screen instructions, and launch WampServer when it’s
done. (Note: There may be a port 80 conflict with your Skype software,
but
there’s a fix for that.)
Upon installation, a www directory will be created automatically. You’ll likely find it here: c:\wamp\www
From that directory, you can create subdirectories (called “projects”
in WampServer), and put any HTML or PHP files inside those
subdirectories. If you click on the localhost link in the WampSever menu
or open your internet browser with the URL http://localhost, you should
be shown the main screen of WampServer.
Step 2: Creating an HTML Page and Configuring MySQL
To test our WampServer, we can put an example file called “info.php”
into our www-directory. Go directly to this directory by clicking “www
directory” in the WampServer menu. From there, create a new file with
the sample code like
<title>PHP Test</title> and save it.
Now you can browse to http://localhost/info.php to see the details of
your PHP installation. You can create any HTML and PHP file structure
to suit your needs.
If you click on the phpMyAdmin menu option, you can start configuring
your MySQL databases (which may be needed for a CMS like WordPress).
The phpMyAdmin login screen will open in a new browser window. By
default, the admin username will be root, and you can leave the password
field blank.
From there, you can
create new MySQL databases and alter existing ones. Most software, like WordPress, will automatically set up a new database for you, though.
Step 3: Make the Site Public
By default, the Apache configuration file is set to deny any incoming
HTTP connections, except in the case of someone coming from the
localhost. To make your site publicly accessible, you need to change the
Apache configuration file (httpd.conf). You can find and edit this file
by going to the WampServer menu, clicking Apache, and selecting
httpd.conf. Find these two lines of code:
|
Order Deny,Allow
Deny from all
|
And replace them with these:
|
Order Allow,Deny
Allow from all
|
Restart all WampServer services by clicking “Restart All Services” in
the menu. The site should now be accessible from beyond your localhost.
Confirm there isn’t a PC firewall blocking web requests. You may need
to
set up port-forwarding on your internet router as well.
Step 4: Using a Domain Name
To use a domain name with your WAMP installation, we’ll need to
configure some files first. Let’s assume that our example.com domain has
an A record in your DNS with the IP address 100.100.100.100.
First, we need to add the following line to the C:\Windows\system32\drivers\etc\hosts file:
100.100.100.100 example.com
Next, we need to edit httpd.conf again (accessible via the WampServer
menu) to add a virtual host. Once that file is open, look for “Virtual
hosts,” and uncomment the line after it, like this:
|
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
|
Now we need to add a file manually in
“C:\wamp\bin\apache\Apache-VERSION\conf\extra\” (VERSION is your Apache
version). Create a file in Notepad, or your text editor of choice. Add
the following code, and save the file in that Apache directory.
|
ServerAdmin mail@example.com
DocumentRoot "c:\wamp\www"
ServerName mysite.local
ErrorLog "logs/example.com.log"
CustomLog "logs/example.com-access.log" common
|
Click “Restart All Services” in the WampServer menu to activate these
changes, and confirm that your site is accessible via its domain name.
Voila!
Linux: How to Host Your Own Website on a Linux Machine
Let’s now cover how to set up Apache, MySQL, and PHP on a Linux
system. LAMP stacks are far and away the most common Linux hosting
configuration but don’t be afraid to play with NGINX or LiteSpeed
webservers, either.
Step 1: Install Your Software Using the Terminal
To start our LAMP software install, type the following in the terminal:
|
sudo apt install apache2 mysql-server php libapache2-mod-php7.0
|
During the installation process, you will be asked to enter (and
re-enter) a password for the MySQL root user. Technically, it’s not
necessary (as it should have been done upon installation), but just to
be sure, we will restart the Apache webserver. Any time you change the
global configuration of Apache, you need to execute the command below,
unless you do the configuration using local .htaccess files.
|
sudo /etc/init.d/apache2 restart
|
Step 2: Check PHP
To confirm your PHP server works and see what PHP modules are
currently available, you can place a test PHP file in the webserver root
directory (/var/www/html/):
|
sudo echo "" > /var/www/html/info.php
|
We can now visit that PHP page by browsing to http://localhost/info.php.
You should see the currently running PHP version, current
configuration, and currently installed modules. Note that you can later
install other PHP modules using the Ubuntu package manager, as some PHP
applications might require that. To determine which extra modules are
available, search within the
graphical package manager, or simply use the command line:
|
apt search php | grep module
|
Step 3: Check MySQL
As most content management systems, including WordPress, Joomla, and
Drupal, use MySQL databases, we should also confirm our installation
worked and the software is fully updated. To see if your MySQL
installation is working, type
service mysql status into the terminal.
|
$ service mysql status
● mysql.service - MySQL Community Server
[...]
mrt 15 13:24:09 host1 systemd[1]: Started MySQL Community Server.
|
If you don’t see that the MySQL Community Server started, you can type
sudo service mysql restart to restart the MySQL server. From here, we can use the MySQL command line client to manage databases.
For this, we need to use the admin credentials we typed earlier when MySQL was installed. Reach the login prompt by entering:
$ mysql -u root -p
Often times, the CMS will automatically create the database for you,
but sometimes you need to do something to the database manually.
PHPMyAdmin is a friendly database management tool most web experts will recommend and can be installed with:
sudo apt install phpmyadmin
Finally, configure the /etc/phpmyadmin/config.inc.php file using the steps described
here.
Step 4: Configure DNS
To use your own domain for your local web server, you’ll need to
configure Apache to accept web requests for your domain. First, make
sure your domain’s DNS has an A record (which points to a specific IP
address) for your domain name, e.g., www.example.com. Your DNS hosting
provider will have online tools to correctly set up these DNS records.
Once that is done, you should be able to see something like this using the
dig tool. To request the A record for www.example.com, type:
|
$ dig www.example.com A
;; ANSWER SECTION:
www.example.com. 86400 IN A 100.100.100.100
;; AUTHORITY SECTION:
example.com. 86398 IN NS a.iana-servers.net.
example.com. 86398 IN NS b.iana-servers.net.
|
Here, a web link for http://www.example.com would be directed to the server with IP address 100.100.100.100.
Step 5: Configure Apache
Now, we need to tell Apache to accept web requests for our name
www.example.com and from what directory to serve content when we get
those requests. To do that, we’ll set up a directory for our example.com
domain and create an example index.html file before setting some
filesystem permissions:
|
sudo mkdir -p /var/www/html/example.com
sudo sh -c 'echo "
<h1>This is my self-hosted site example.com</h1>
" > /var/www/html/example.com/index.html sudo chmod -R 755 /var/www/html/example.com
|
To see this page, the last step is to set up a Virtual Host file for Apache for our domain.
|
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
|
|
sudo nano /etc/apache2/sites-available/example.com.conf
|
Now edit the file to look like this (the optional comments are not shown here):
|
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
|
Now reload Apache for the changes to take effect:
|
sudo a2ensite example.com.conf
sudo service apache2 reload
|
Edit your local /etc/hosts file by adding a line with your IP address
and domain name. Be sure to change the “100.100.100.100” to match the
IP address of your domain:
|
100.100.100.100 example.com
|
We should now be able to visit our self-hosted site by going to http://www.example.com.
source by:hostingadvice.com
Comments
Post a Comment