Top Banner

of 11

How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

Apr 07, 2018

Download

Documents

johodada
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    1/11

  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    2/11

  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    3/11

  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    4/11

    wondering, it stands for super-user. If you ever forget who youre logged in as, the whoami command will tell

    you. Or look at the command prompt, which will end with $ for a normal user and # for the root user.

    Some Useful Commands

    Here are some useful commands to get you started, now that youve got a usable Linux system:

    shutdown h now turns off the computer (h means halt)

    shutdown r now reboots the computer (r for restart)

    exit logs you out. Youll need to do this twice if you used su. Remember that the web server is still running when

    you log out, so web/telnet connections to it will still work just fine. Theres no need to remain logged in all the time.

    ls shows a directory listing (that's LS).

    ls la shows a better one (that's LS -LA).

    cd / switches to the top-level directory.

    cd dirname switches to the specified directory name, eg cd /etc.

    clear clears the screen, like cls does in Windows.

    cat is the linux version of the Windows "type" command if you want to display the contents of a text file.

    rmdeletes a file

    cp is the linux equivalent of the DOS/Windows copy command.

    find / -name xyz.ext will search the entire system for a file named xyz.ext

    pwd (print working directory) tells you which directory youre currently in

    Within an ls la directory listing, lines that start with a "d" are directories (folders), otherwise theyre files. The

    other characters at the start of the line (such as rwxr--rw-) tell you who has permission to read, write, and execute

    the file. A google search for chmod will tell you how to understand and change these.

    Get Updated

    Now we need to scan the internet for any important updates. Start by using thesu command, because you need

    to be root in order to do this.

    Type apt-get update to update the catalog of possible updates.

  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    5/11

  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    6/11

    Type cat shells to check that the file seems OK

  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    7/11

    Type cat shells to check that the file seems OK.

    Each user has a home directory which contains their various files. Its like My Documents in Windows and normally

    it resides in the /home directory. For web users, rather than setting their home directory to be somewhere within

    /home well put it under /var/www, which is the root of the web server. Files under /var/www are served by the

    web server (apache) and sent to visitors' web browsers. Files that aren't within /var/www are not accessible in this

    way, so there's no point in web site authors putting them anywhere else. Such users have no need for a directory

    within /home, as they won't be creating work that is only for use within the server and which won't need to be

    shared with anyone else.

    Lets make an account for a user called webuser1 with a password of flintstone. These are the steps that you need

    to do for each web user account you want to create:

    cd /var/www

    mkdir webuser1

    useradd webuser1 p xxxx d /var/www/webuser1 s /bin/false

    chown webuser1 webuser1

    passwd webuser1

    Then, when asked, choose flintstone as the password.

    Note that xxxx above is your root password, NOT the one that you want to assign for the webuser1 account.

    Also note the chown command which changes the ownership of the webuser1 directory from root (which created

    it) to webuser1. If you dont do this, webuser1 wont be able to upload files.

    Verify that you cant telnet to the server using the webuser1/flintsone account. The connection will start, but will

    immediately exit again.

    Now create a simple index.html file and use ftp to upload it, using the webuser1/flintstone account. Then surf to

    http://192.168.1.10/webuser1 from any machine on your LAN and you should see the uploaded page.

    We now have a working web server with an ftp server, and the above-listed set of commands allow you to create

    new user accounts for your web server.

    Before we leave proftpd, there are a couple of changes that we need to make to its configuration file in order to

    improve security and make things neater.

    Type cd /etc/proftpd and then vi proftpd.conf to edit the config file. Move the cursor up and down

    with j and k until you reach the DefaultRoot line, and remove the # symbol from the start of the line by pressing

    the x key. This will lock all ftp users into their home directory (eg /var/www/webuser1) and wont let them view

    files that are further up the tree. Without this step, our webuser account holders could use their ftp software to

    browse the entire server's directory structure

  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    8/11

    browse the entire server s directory structure.

    You may also wish to change the ServerName entry from Debian to the name of your server, to make the welcome

    message more relevant. With vi, remember that typing i puts you into insert mode, for typing text, and Esc then

    puts you back into command mode from where you can type :w to save the file and :q to quit vi.

    Webmin

    Now that ftp is working, lets install Webmin so that we can remotely administer the server from anywhere on our

    LAN via a web browser. Its more fun and friendly than using telnet, and a great way to explore the machine.

    First, make sure youre logged in as root (via your normal user account and su) then type the following 3

    commands. Make sure that each one has finished, and you're back at the command prompt, before typing the

    next:

    wget http://www.webmin.com/download/deb/webmin-current.deb

    sudo dpkg -i webmin-current.deb

    sudo apt-get -f install

    You'll get some error messages after step 2. That's normal. Step 3 fixes the problems.

    Webmin should now be installed and running. From another machine on your LAN, surf to

    https://192.168.1.10:10000 and log in as root, using your servers root password. Note the https bit it wont work

    with plain http. Also note the :10000, which is essential. Ignore any warning from your web browser about a

    missing SSL security certificate you can trust this server unconditionally because its yours.

    Webmin is a hugely powerful, free tool that allows you to manage, administer and examine every aspect of your

    server via a web interface, so feel free to explore it.

    Possibly the most useful part of Webmin is the file manager, which also lets you edit files. Youll find it in the

    others category of the left-hand menu, though be aware that you need Java installed on the computer from which

    you're accessing the server (not on the server itself) in order to make the file manager work.

    Webalizer

    Now well install Webalizer, which is a great tool that produces graphical stats to show your web site usage. Even if

    youre only using your server for test/educational purposes, its useful to be able to see the sort of stats that are

    available with such programs.

    To install webalizer type apt-get install webalizer

    You need to tweak the Webalizer config file before the program will work.

    Type cd /etc/webalizer then vi webalizer.conf and delete the .1 from the end of the

    LogFile entry. It's around the 25th line of the file, from the top.

  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    9/11

    g y , p

    Webalizer produces its reports by analyzing the Apache web server log file on a regular basis. To make it do this,

    you need to set up whats called a cron job (the Linux version of a Windows scheduled task) in order to run /usr/bin

    /webalizer regularly. Every 15 minutes should do nicely, and the easiest way to do this is via Webmin.

    Go into Webmin via https://192.168.1.10:10000 from another PC and, under the System category, click on

    "Scheduled Cron Jobs". Then click "Create A New Scheduled Cron Job".

    Choose to execute the job as root. The command to execute is /usr/bin/webalizer. Click on "Times And Dates

    Selected Below". Under the minutes, tick "Selected" and choose 0, 15, 30 and 45. For hours, days, months and

    weekdays, select "All".

    Now click the Create button and close your web browser. After 15 minutes or so, surf to http://192.168.1.10

    /webalizer and you should see the reports and stats. Wait another 15 minutes and you should see an updated

    version.

    PHP and MySQL

    Now we need to make PHP and MySQL work, to ensure that we can host not just static html sites but also dynamic

    database-driven ones. PHP should already be working just fine, so we need to test that. Create a file called test.php

    which contains:

    Upload it using the webuser1 account. Surf to http://192.168.1.10/webuser1/test.php and check that you see a web

    page containing just the message this is a test file. If it works, PHP is working on your web server.

    To allow users to create database-driven sites well install phpMyAdmin, which is a graphical web-based tool for

    managing MySQL databases. Its best if we dont allow web users to create their own databases, but we do want

    them to be able to manage the databases that we set up for them. PhpMyAdmin will work for both of these tasks.

    IE, for us to create databases and for our web users to maintain the tables within their allocated database.

    As root, type apt-get install phpmyadmin

    When asked which web server youre using, choose apache2.

    You'll be asked whether you want the system to set up a config database with dbconfig-common. Say yes.

    Next, you'll be asked for the MySQL root password, which phpMyAdmin needs in order to connect to the database

  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    10/11

    192.168.1.x, the router is on .1, and the DHCP server is configured to issue addresses from .30 to .50. In which

  • 8/6/2019 How to Build Your Own Webserver Using Ubuntu Server 11.04 Natty

    11/11

    case, you could use .60 for the server. So that's what we'll do.

    Log into the server, then type su to give yourself root privileges.

    Now type vi /etc/network/interfaces to edit the network config file.

    There's probably only one wired ethernet network connection, and it's probably labelled eth0. So you should see

    something like this in the file:

    auto eth0

    iface eth0 inet dhcp

    This confirms that the network interface is currently set to use DHCP.

    Change dhcp to static, so that the line reads iface eth0 inet static. Then add the following 3 lines

    underneath:

    address 192.168.1.60

    netmask 255.255.255.0

    gateway 192.168.1.1

    If the netmask on your LAN is different, then enter the correct one. Equally, if your gateway (the address of your

    router) is different, amend the line accordingly. Assuming there's a Windows PC on your LAN, you can normally

    find out both of these settings by typing ipconfig /all from a Windows command prompt.

    Save the file (type :w then :q) and then reboot the server by typing shutdown -r now. You should then find

    that you can now telnet to the machine at its new, permanent IP address.

    And that's it. You now have a fully working web server that you can use for test, development and training

    purposes. Have fun.

    Now that you have a web server at your dispoal which is capable of hosting PHP/MySQL sites, all you need is a way

    to find out how to create such things. In which case, check out www.the-web-book.com , which is a 350-page

    ebook, written by me, that tells you all you need to know. You can purchase the downloadable PDF version via

    PayPal, or browse it on-screen for free.

    Copy r igh t Robe r t S ch i f reen . Las t upda ted Ma rch 2011

    T he of f i c ia l home o f th i s docum en t i s w w w . t h e - w e b - b o o k . c o m . I f you wan t to te l l peop le abou t th i s

    documen t , o r l i n k to i t , p lease use tha t URL ra the r than any o the r .