This article was written by me based on frequent questions from the support service of the hosting provider RunWeb.Ru (to which I am directly involved) on testing scripts and Apache settings on a home computer. Initially, this material was published on the server RunWeb.Ru and in the mailing list “PHP for beginners.” Gradually, it expanded as questions were received from our users. When I had enough information, I gathered it in a separate article and decided that it could be useful not only to our hosting users, but also to the rest of the network’s residents.
The reason for the article was the desire to create a unified guide for users, which would help them to test scripts at home, as well as use the features of the Apache settings for further use on our server.
A couple of words about webserver work
By installing a web server, as described in this article, you get a fully functional server with a full set of features. What is it, why now you can access the address “127.0.0.1” and not even connect to the Internet? I am sure the newbies will be asked by these questions and many others. Here I want to explain a little bit how web servers work and tell you more about the structure of sites on the network.
You probably know that when you type the address in the browser line, you are working via the http protocol (hypertext transfer protocol). This is understandable at least in the address bar – it looks like http: //www.address.ru.
What is the protocol? This is a set of commands with the help of which your computer (specifically your browser) and the web server that you access are communicating. To create websites it is very important to understand this moment.
And what is a web server? First of all, it is a computer on which the software is installed, giving the opportunity to transfer information to you when requested by your browser. This software is Apache installed by us, which just knows how to work using the http protocol. When you start Apache, someone appears on your system who has something to respond to the browser when they request a specific address. That is, it turns out that you have a server that stores the site (or sites) and software that can transfer these sites to the browser. So they communicate with each other in the language (protocol) http – browser and Apache. The browser requests files from Apache, and Apache gives them to it.
Now about how the sites look on our server. All sites are stored in an orderly manner, each in a separate directory. The site that opens at the address “http://127.0.0.1” has “F: \ project” as the root directory. The directory can be called as you like and is the root directory for this site. Directory names and their correspondence to http-addresses are recorded in the Apache configuration file – “httpd.conf”, which we will configure below. To understand this, imagine that you can create a directory “F: \ site” and write in the file “httpd.conf” that this is a real site and it should open at “http://127.0.0.2”. And it will work. (How to do this, we will study later).
Usually there are several more directories in the root directory of the site – for storing html files (this is our www directory), for storing statistics of access to the site (this is the logs directory) and others, while we are unused for a small need (for example, the cgi-bin folder for storing programs written in PERL). So, the folder that can store html-pages, i.e. Our www folder can also contain php programs. That is, everything that is put into this folder will be visible at the address “http://127.0.0.1”. This means that if you create the file “F: \ project \ www \ page.html”, then you will see it at the address “http://127.0.0.1/page.html”. Having created the catalog “F: \ project \ www \ katalog”, you can look at it at the address “http://127.0.0.1/katalog/”.
In addition, each directory on the server can have a so-called. “index file”. This is the file that opens by default when the directory is called, i.e. without explicitly specifying the file name. In our case, this is index.html, which is located in the root directory of the server. By typing “http://127.0.0.1” we see this particular file. Accordingly, we can also see it by typing “http://127.0.0.1/index.html”.
Installing the Apache web server and PHP language
In order to install a web server on your computer, you need some software. All software that we will use is completely free from birth and is available online for free download. This, you see, is good, especially if we consider that there are very few free programs (especially of such a serious level) for Windows. It is also noteworthy that you do not have to download pirated software and dig into the network in search of cracks / serial numbers, etc.
Apache Web Server
Apache is a program that performs the functions of an http server. It is with its help that the web server will function. This program performs all the necessary functions, under its leadership, most of the network resources work. Ours is no exception, because of the flexibility and versatility of Apache, we will use this particular server. In the future, you will see how easy it is to use this program at home and how extensive it provides. Before you start installing the server, you need to immediately download Apache. As a version of Apache, I propose to use 1.3.12, which has a full set of functions and is not complicated at the same time by unnecessary improvements at this stage. In addition, it is about 2 MB less than newer versions. You can download it directly from our server RunWeb.Ru:
Download Apache: http://RunWeb.Ru/download/apache_1_3_12_win32.exe (3.1 MB)
PHP programming language
This program will give you the opportunity to use PHP-scripts on sites created within the Apache server. As a version I recommend PHP 4.3.4 as the most promising and stable version. In addition, this version contains rich features compared to previous ones. Download it also from the server hosting provider RunWeb.Ru:
Download PHP4: http://RunWeb.Ru/download/php-4.3.4-Win32.zip (6.0 MB)
As you can see, both programs are quite large. However, nothing can be done about it, especially since downloading them once you save them for a long time and can use them in the future. So, if everything is loaded, you can proceed to the installation.
For the server and all our future projects, it is advisable to allocate a separate disk on your computer. Of course, you don’t have to buy a new hard drive, we’ll have enough virtual disk capabilities. What is a virtual disk? This is an imitation of the hard disk in the system. If you are familiar with the possibilities of MS-DOS, you already understand what I mean. A virtual disk has its own separate name in the system (for example, disk F 🙂 and uses the usual directory on the main disk as its root directory. A virtual disk is created with the following MS-DOS command:
subst F: C: \ server
Where F: is the name of the virtual disk, and C: \ server is the folder on the primary disk, which will be the root directory of the new disk F. Now let’s create a new disk. To do this, follow these steps (only for Windows 95-98. How it is done in other versions, see below):
Create a disk C: folder “server”. Open the “Run” menu in Windows, which is located on the “Start” button and type the line “subst F: C: \ server”. This should open a window with the root directory of your new disk. Please note that both here and further I will use the F: drive as the server and the C: \ server folder as the root directory of this drive. I strongly advise you to use these parameters in order to avoid confusion in the future. In the following examples, I will proceed from the fact that you have everything configured exactly that way.
Note that the virtual disk disappears after a reboot and the subst command must be run again. In order not to call this command every time, you need to write it to the file autoexec.bat, which is located in the root directory of the system disk. This file contains a list of all the programs that must be executed when the system boots. So, open this file in Notepad and write down the following line at the very end:
subst F: C: \ server
However, on some operating systems this may not work (for example, Windows ME, XP …). This is due to the fact that the autoexec.bat file belongs to MS-DOS, and in later versions of Windows it simply does not exist. In this case, do the following. Create a file “subst.bat” and write in it only one, all the same line “subst F: C: \ server”. Now place this file in the root directory and add its shortcut to startup. This approach will lead to the execution of the command file subst.bat each time Windows starts. But there will be an unpleasant moment. When you run the file, the MS-DOS emulation window should open, in which the file will run. It is not very convenient to close this window every time you start the system. So it may be better to run the file subst.bat separately if necessary.
All is ready? Then let’s continue the installation. The first thing we need to do is install an Apache web server. The program you downloaded is an installer and copies all the necessary files itself. However, the rest is not so smooth and it will not be enough just to complete all installation steps. To install the server exactly as we need, let’s proceed as follows.
Run the Apache installation file. After running the installer, press the “NEXT” and “YES” button until the queue reaches the installation path. The default path is “C: \ Program Files \ Apache Group \ Apache”. It does not suit us at all – you need to install Apache on the newly allocated drive F. To do this, click “Browse” and specify the path “f: \ usr \ apache” (if there is no such directory, the system will ask if you need to create it, which is affirmative). Why choose “F: \ usr \ apache”? The fact is that the “usr” directory on the server is a service one, which lies separately from the directories where the site files and other server elements are located. The “usr” directory on the server is like the “Windows \ System” directory on Windows. Next you will come across this directory when administering your server. Click “NEXT”, select “Typical” – the usual installation. We are waiting for a couple of times “NEXT” and the server is finally installed on our computer, performing copying files. Everything! You have just installed a personal web server on your computer where you can create an unlimited number of sites with unlimited possibilities.
However, with the creation of sites, we still wait. Before that, you need to install PHP and a lot of things in the settings of your server. This is not difficult – we will not go into serious Apache settings today (by the way, this is a topic for a whole book!), We will only perform the actions that are necessary and sufficient for us to complete the work of the home server.
We continue. Now we need to install PHP. You have a file downloaded from the RunWeb.Ru server. This is no longer an installer – this is a regular ZIP archive. Create a folder “php” in the same directory “F: \ usr \” and unzip it into the resulting directory “F: \ usr \ php”.
Now look at the contents of the directory “F: \ usr \ php”. Do you see the php.exe file and along with it the ddl, extensions, java, Mibs, etc. folders? Yes? Fine! Congratulations, PHP is ready to execute your commands. It remains only to introduce Apache with PHP – by the way, they form a perfectly working pair.
Configuring Apache and PHP server
With the installation program is over. We now turn to their configuration. First of all you need to find the file “php.ini-dist” in the directory “F: \ usr \ php” and copy it to the C: \ Windows folder, and rename it in the new location to the file “php.ini”. As a result, you should get the file “C: \ Windows \ php.ini”. Let’s leave it as it is, change nothing in it and move on to configuring Apache. Everything is a little more complicated here.
Open the file “F: \ usr \ apache \ conf \ httpd.conf” in some text editor, for example, Notepad. This is a very important file that contains all the settings of your server. It is necessary to set several parameters. Let’s start.
1. Find in the httpd.conf file the line “#ServerName new.host.name”. Change it to the line “Servername localhost”. Please note that the “#” sign at the beginning of the line is removed.
2. Next, find the line “DocumentRoot” f: / usr / apache / htdocs “and change it to” DocumentRoot “f: / project / www”. Notice the _notting_ slash at the end.
3. Now find the line “<Directory” f: / usr / apache / htdocs “>” and change it to “<Directory” f: / project / www “>. The slash is again missing.
4. Also find and change the line “ErrorLog logs / error.log” to the line “ErrorLog F: /project/logs/error.log” and the line “CustomLog logs / access.log common” to “CustomLog F: / project / logs /access.log common “.
5. Now go to the very end of the httpd.conf file and put the following lines in it:
ScriptAlias / php / “f: / usr / php /” AddType application / x-httpd-php .php .phtml .php4 Action application / x-httpd-php “/php/php.exe”
These lines add to the Apache settings the ability to execute files with the .php, .phtml or .php4 extensions as programs written in PHP (PHP scripts).
6. Well, now create the directory “F: \ project”, and there are two directories “www” and “logs” in it. As a result, you should get the following directory tree:
F: \ project
F: \ project \ www
F: \ project \ logs
Everything! Congratulations, you have installed and configured a full-featured http-server with PHP support. Now it’s up to you to learn how to turn the server on and off and test it a little bit. This we will do next.
Work with Apache server
When installing the server, the installation program created the folder “Apache Web Server” in the Main Menu (“Start” button, hereinafter referred to as “Programs”). There are several shortcuts in this folder. We will need the following ones:
Start Apache – to start the
Stop Apache server – to stop the server
If you wish, you can copy them to the Desktop to be able to quickly start and stop the server.
We move further and come to the most important point – the launch of the server. If you have performed all of these actions, then you will definitely overcome this stage without any problems. But before launching, I would like to suggest that you do something else in order to test your server for sure.
So, create in the folder “F: \ project \ www \” a file named “index.html” with the following contents:
<html> <head> <title> Server Main Page </ title> </ head> <body bgcolor = # ffffff> Congratulations, the server is working! <br> Test PHP: <a href="test.php"> test.php </a> </ body> </ html>
Create in the same place a file with the name “test.php” with the following content:
<? phpinfo (); ?>
That’s all, you can finally start the server. Solemnly launch the Start Apache shortcut. This opens a black window like an MS-DOS window with a single “Apache / 1.3.12 (Win32) running …” sign. This means that the server has been successfully started and is currently running. You do not need to close the window, it is the Apache working environment and is closed using the “Stop Apache” shortcut, which turns off the server.
So, the server is running. What’s next? Open your browser and type in the address “http://127.0.0.1”. Remember, you do not need to connect to the Internet at this moment! You should see the greeting already typed, i.e. code file “index.html”. Follow the link to the file “test.php”. What do you see? The browser should display a fairly large file with a bunch of different notations and the proud heading “PHP Version 4.3.4” together in the PHP logo. This is the result of the work of our little script “test.php”, or rather the phpinfo () functions, which we wrote in it. This function displays all the parameters of PHP installed in the system.
If all this seemed complicated and incomprehensible to you, do not be intimidated. Further it will be much easier, just for the first time it was necessary to install and configure everything to work. Now you have the necessary minimum, which is enough to successfully start working with the server.
Separately, I want to turn to advanced users who, I am sure, read (or will read) my article. First of all – if you are not satisfied with the amount of information (and explanations for it) about Apache settings and various PHP directives – you shouldn’t blame me for that. My goal in this article was to provide beginners with a launching pad for their training and first project. In the future I will try to continue this topic.
Example: Configuring a Virtual Host
At the moment there is only one host on the created server, with the address already known to you http://127.0.0.1. However, if you have several sites, it is not always convenient to use one host to debug them. For these purposes, Apache can use several virtual hosts.
So, let’s start creating a virtual host. As you already know, all Apache settings are made in the httpd.conf file, which is located at F: \ usr \ apache \ conf \ httpd.conf. In this file there is a special section, which is designated ### Section 3: Virtual Hosts . It is located at the very end of the file and is responsible for virtual hosts. Next, the process of creating a host in steps:
1. Turn off Apache.
2. At the very end of the httpd.conf file, add the following lines:
NameVirtualHost 127.0.0.2 <VirtualHost 127.0.0.2> ServerAdmin admin @ site2 ServerName 127.0.0.2 DocumentRoot "/ site2 / htdocs /" ScriptAlias / cgi-bin / "/ site2 / cgi-bin /" ErrorLog /site2/error.log CustomLog /site2/access.log common </ Virtualhost>
In this directive, we describe a new host. The string VirtualHost 127.0.0.2 denotes the address of the new host. ServerAdmin admin @ site2 specifies the administrator’s Email that will be displayed in case of errors and server service messages. ServerName 127.0.0.2 denotes the name of the host by which we will access it from the browser. DocumentRoot “/ site2 / htdocs /” specifies the directory where the html and php files of the virtual server are located. ScriptAlias / cgi-bin / “/ site2 / cgi-bin /” specifies the directory in which cgi-scripts are located (programs on perl, c ++, etc.). ErrorLog and CustomLogindicate the location of the server log files. ErrorLog is an error log, CustomLog is an access log. All errors will be recorded in these files (for example, when the requested file does not exist) and all requests to the server.
3. Now create the described directories F: \ site2 \ htdocs and F: \ site2 \ cgi-bin. The error.log and access.log files can be not created, they will be generated automatically when Apache starts. The F: \ site2 directory is the root directory of the virtual server. Please note that if you do not create or make a mistake in the directory names, then Apache will refuse to start.
4. Now you can run Apache. Remember to save the changes in the httpd.conf file.
That’s it, a new host has been created. You can place files in it and view them through a browser at http://127.0.0.2.
Configuration file .htaccess
An .htaccess file is a webserver service file with which you can make certain settings for your site. For example, using this file, you can customize your error pages or process SSI directives.
The .htaccess file can be placed in any server directory. In addition, it acts on all internal directories located in the current directory. That is, if you have the “www” directory and there are two directories “1” and “2” in it, then by placing the .htaccess file in the “www”, you simultaneously extend its effect to the “1” and “2” directories.
Note that the name of this file starts with a dot. Files whose names begin with a dot are hidden in Unix. This means that if you upload this file to the provider’s server, you will not see it in your FTP client. This, by the way, is a frequent question that we encounter in the RunWeb.Ru support service. What to do if you want to change this file? The easiest option is to change it on your home computer and upload it to the server via FTP again. Your client should not ask whether to overwrite it, since it does not see .htaccess in the file list. This is normal, you can be sure the file will be overwritten.
As an example of using .htaccess, let’s look at setting up your error pages. What is “your error pages”? Suppose a server is requested a document that does not exist. In this case, the error number 404, “File not found”. Apache sends its service page to the browser (404 Not Found). The same with error number 500 “Internal server error”. It occurs when an error occurs on the server, for example, when executing a CGI script. So how to fix your error pages?
To do this, create an empty .htaccess file in the root directory of the virtual host (for example, F: \ server) and write the following lines into it:
ErrorDocument 404 http://127.0.0.1/err404.html
ErrorDocument 500 http://127.0.0.1/err500.html
They assign each page its own error, which will open when this situation occurs. Naturally, you will need to create the err404.html and err500.html files on the server. They can be called whatever you like, not necessarily as in this example.
There are also many other options for customizing the .htaccess file. Several very useful features (such as installing an index file, password-setting directories, SSI settings, and others) are described on our RunWeb.Ru server in the FAQ about server settings . The list is often updated, so I recommend that you familiarize yourself with the features described in it.
Frequent installation questions
1. There are no shortcuts to starting and / or shutting down the server.
Strange, of course, but it also happens. In this case, you need to create shortcuts to start and stop the server manually. They should be:
Starting the server: F: \ usr \ apache \ Apache.exe Stopping the server: F: \ usr \ apache \ Apache.exe -k shutdown
2. The file F: \ usr \ apache \ conf \ httpd.conf is missing
This happens and is related, as well as the previous point, to various Apache distributions. (I advise you to download the distribution kit shown here!). So, the file “httpd.conf” is missing, but there is a file “httpd.conf.default”. In this case, you need to copy the file “httpd.conf.default” into “httpd.conf” (just create a copy of this file with a different name, rather than rename the file itself. “Httpd.conf.default” is always desirable, since it is distribution of Apache installations and with it you can always return to the initial settings). Well, then make all the settings in the resulting file.
3. PHP does not work (code is displayed in the browser) or Apache does not work (the window closes immediately).
Check the installation and configuration of the server (see above). All paths, settings, scripts texts must necessarily coincide with the examples – the letter in the letter. Otherwise, the problems are likely to be. For example, you put a slash in the settings in the other direction (instead of “/” wrote “\” or vice versa) or in the text of the script instead of “<?” wrote “<?” (with space), etc. Unfortunately, it is very difficult for me to calculate such errors … at least I need to sit at your computer 🙂
4. The MS-DOS window when you start Apache opens and immediately goes out. I do not even have time to read what is written there.
This means that Apache is incorrectly installed or configured. Do not immediately repeat the installation (run setup) – the server files are copied correctly, because the shortcut opens the Apache.exe file. In this case, the server settings are usually incorrect. Check the file “httpd.conf” in the directory “F: \ usr \ apache \ conf \”. Look especially at the paths (F: \ project, etc.). Do not forget the slash! They should be exactly (in that direction), as shown in the example (see archive). And, it is very important: where there are none (for example, at the end of the paths) you do not need to install them! Finally, check that the directory structure is correct on the server.