Jump to content
Larry Ullman's Book Forums

Configuration Of Ex2 Using Xampp


Recommended Posts

I am having trouble configuring ex2 using xampp. I don't seem to be able to see the root directory. Would somebody please explain to me the relationship of the ex2 zip file and xampp. What configuration do you use? Why is there a html directory. Why can't I find root directory files such as mysql.inc.php from the php code? Amazingly enough, I understand the code just fine. I just don't know how to configure my workspace. Thanks for you time and help.

Link to comment
Share on other sites

Thanks Larry. The problem with that configuration for me is that it assumes I do not need to install a second project. I have created a directory cp where I have put the files from ex2/html. However, I don't know how to get apache to recognize the home directory is //localhost/htdocs/cp. It means I have to do relative includes which I don't want to have to do on my website. I looked in php.ini for a setting of the root directory for xampp but could not find it.

Link to comment
Share on other sites

I think what your trying to do isn't possible. If I understand correctly.

htdocs and localhost are the same thing in round about ways - basically the root. But as you say that stops you using more than one project efficiently at a top level.

 

I think i'm right in saying that most people would have to do the following:

                      xampp
                        |
                      htdocs
            ____________|________________
           |            |                |
            cp   another project   other project

inside htdocs, store each project in it's own folder (if you intend to have more than 1 project in xmapp that is, if you onyl have one then you can leave it in htdocs.

 

Then to access your projects the url would be: http://localhost/cp/ not http://localhost/htdocs/cp

 

Does that help?

 

In what way can't you get Apache to recognise the home directory??

  • Upvote 1
Link to comment
Share on other sites

In a cPanel based host this is how the web directory structure is setup.

 


         home   <-- Users, which would be you, your scripts etc... do NOT have access to this folder.
           |
        username  <-- This is the base folder for your website, it is NOT web accessible, in non-reseller hosting this one user would control ALL your sites.
           |
       public_html  <-- this is web accessible folder, in non-reseller hosting ALL your sites would be located in sub folders off of this folder.

 

 

I do NOT use XAMPP or preinstallers that setup everything for you, I used to but they can't handle custom installs with multiple instances of php, mysql etc etc... The instructions below are for adjusting Apache via httpd.conf and/or httpd-vhosts.conf files. These maybe named different in your install.

 

To get httpd.conf setup to use multiple folders where each folder is a new project you would put the following in httpd.conf OR vhosts.conf or httpd-vhosts.conf. I do NOT know what file your setup uses for the virtual hosts but there should be a link or something in your control panel XAMP uses to give you access to the correct location.

 

You can also find it manually by going into where ever apache is installed on your hard drive and looking through the apache\conf folder for httpd.conf and scroll towards the bottom of the file somewhere and see if there is a section that starts off with

 


#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
#NameVirtualHost *:80

 

If you do not see this then there maybe a link that looks similar to

 

 


# Virtual hosts
Include conf/extra/httpd-vhosts.conf

 

 

Find this file if this link exists in httpd.conf and open it in notepad or something similar, do not use wordpad as it adds hidden junk characters to the file.

 

Now my setup is going to be a little different to yours, so make changes based on your setup. I will explain line by line best I can.

 

I am going to leave out the commented sections to save space, so pay attention to your file and where these lines are located in it. To be safe you should save a unmodified copy if this file so if what you change doesn't work you can revert back to a working copy. You will need to restart apache once you make the changes or it wont work until you do.

 


NameVirtualHost 127.0.0.1:80  <--  This line defaults at  *:80  this makes ALL ip addresses accessible to your testing server on port 80.  
                                  I hardcode the localhost IP address instead of leaving it open but it will work either way.


<VirtualHost 127.0.0.1:80>       <-- Tells Apache this is a new virtual host and what IP and Port it is on, default is *:80
   DocumentRoot D:\htdocs\home   <-- This sets document root for local host, this is a windows file sctructure, notice no closing slash at end
   ServerName localhost         <-- This is the NAME of the virtual host, in this case it is localhost
</VirtualHost>                       If you type localhost OR it's ip address into your web browser it will now open to the DocumentRoot folder "home"


 

Change the folder above to whatever your base root folder is. If this is already set in your XAMP installation and you are happy with it, don't change it.

 

Next I will show you redirects or multiple URL's to access ONE folder, which would be your project folder.

 

Under the lines above enter the following, changing to fit your setup.

 


<VirtualHost 127.0.0.1:80>
   ServerName ex2     <-- Name of the URL you type in the web browser
   Redirect permanent "/" "http://www.ex2.lcl/"  <-- Actual URL that the ServerName will go to, this has NOT been setup yet, but when it is it will work.
</VirtualHost>

<VirtualHost 127.0.0.1:80>
   ServerName ex2.lcl
   Redirect permanent "/" "http://www.ex2.lcl/"
</VirtualHost>

 

I setup ex2 and ex2.lcl to both redirect to www.ex2.lcl If you want seperate ex2.lcl and www.ex2.lcl then you have to duplicate the line below. Next I setup the actual web url and folder for apache so when you type ex2 into your web browser it actually goes to the right place. The reason I put ex2 in as redirect as it saves typing, you don't have to put that in if you don't want it.

 

 


<VirtualHost 127.0.0.1:80>
   ServerAdmin administrator@ex2.lcl             <-- Email address for server admin, I always add this in though I don't think it needs to be there technically.
   DocumentRoot D:\htdocs\home\ex2\public_html   <-- DocumentRoot, webroot for this project. This folder structure MUST exist
   ServerName www.ex2.lcl                        <-- ServerName is the actual web address for this site, must put here what you would type in web browser
   ErrorLog logs/ex2-error_log.txt               <-- This is Apache error log output for this site, you do not need this here. It is NOT php errors specifically it is ALL errors apache records for this site. This is NOT put in the project folder, it is put in logs folder within apache's main install folder.
   CustomLog logs/ex2-access_log common          <-- This is similar to the error log above. Read apache manual about this log.
</VirtualHost>

 

 

Now if you want both WWW and NON www versions of web URL working for same project site then you need to duplicate the lines above but change the ServerName to ex2.lcl and remove the redirect lines to ex2.lcl.

 


<VirtualHost 127.0.0.1:80>
   ServerAdmin administrator@ex2.lcl            
   DocumentRoot D:\htdocs\home\ex2\public_html  
   ServerName ex2.lcl                        
   ErrorLog logs/ex2-error_log.txt               
   CustomLog logs/ex2-access_log common          
</VirtualHost>

<VirtualHost 127.0.0.1:80>
   ServerAdmin administrator@ex2.lcl            
   DocumentRoot D:\htdocs\home\ex2\public_html  
   ServerName www.ex2.lcl                        
   ErrorLog logs/ex2-error_log.txt               
   CustomLog logs/ex2-access_log common          
</VirtualHost>


--  This code makes ex2.lcl AND www.ex2.lcl go to the same folder which would open the same site.

 

In addition to adding those lines into Apaches VirtualHost section you must add the URL's to the windows hosts file or no redirecting will work at all.

I use LCL extension for my testing server though you can use any extension you want, make up a new one but I would not use .net, .com. .org or real extensions or you will never be able to access those LIVE sites from your computer.

 


#  windows HOSTS file
#------------------------

127.0.0.1 ex2
127.0.0.1 ex2.lcl
127.0.0.1 www.ex2.lcl

 

 

Restart apache and your new site should showup in your browsers. This only works on THE computer you set the above up on.

 

Now to get a new project working you just duplicate everything above and change names and url to whatever the new site will be, except the localhost document root, that is a single entry one time only.

 

You can have as many URL's pointing to as many folders as you want, the folders can be located ANYWHERE on any accessible hard drive, flash drive, cdrom etc...

 

 

Putting it together:

 


# WWW and non www go to same URL, the WWW version
# typing ex2.lcl will ALWAYS redirect to www.ex2.lcl
# so there is NO non WWW version of site.

NameVirtualHost 127.0.0.1:80

<VirtualHost 127.0.0.1:80>
   DocumentRoot D:\htdocs\home
   ServerName localhost
</VirtualHost>

<VirtualHost 127.0.0.1:80>
   ServerName ex2     
   Redirect permanent "/" "http://www.ex2.lcl/"  
</VirtualHost>

<VirtualHost 127.0.0.1:80>
   ServerName ex2.lcl
   Redirect permanent "/" "http://www.ex2.lcl/"
</VirtualHost>

<VirtualHost 127.0.0.1:80>
   ServerAdmin administrator@ex2.lcl
   DocumentRoot D:\htdocs\home\ex2\public_html
   ServerName www.ex2.lcl
   ErrorLog logs/ex2-error_log.txt
   CustomLog logs/ex2-access_log common
</VirtualHost>

 

 


# WWW and non www go to same folder
# typing ex2.lcl will open the example 2 website
# There is BOTH WWW and NON WWW version of site.

NameVirtualHost 127.0.0.1:80

<VirtualHost 127.0.0.1:80>
   DocumentRoot D:\htdocs\home
   ServerName localhost
</VirtualHost>

<VirtualHost 127.0.0.1:80>
   ServerName ex2    
   Redirect permanent "/" "http://www.ex2.lcl/"  
</VirtualHost>

<VirtualHost 127.0.0.1:80>
   ServerAdmin administrator@ex2.lcl
   DocumentRoot D:\htdocs\home\ex2\public_html
   ServerName ex2.lcl
   ErrorLog logs/ex2-error_log.txt
   CustomLog logs/ex2-access_log common
</VirtualHost>

<VirtualHost 127.0.0.1:80>
   ServerAdmin administrator@ex2.lcl
   DocumentRoot D:\htdocs\home\ex2\public_html
   ServerName www.ex2.lcl
   ErrorLog logs/ex2-error_log.txt
   CustomLog logs/ex2-access_log common
</VirtualHost>

  • Upvote 1
Link to comment
Share on other sites

ex2/html

 

 

Everything in the html folder is the actual example 2 WEB ROOT. Larry just has things organized neatly in the ZIP files.

 

Extract the contents of the html folder to your cp folder, which is your web root.

 

//localhost/htdocs/cp

 

The setup I use in my testing environment is:

 


   home
     |
    ex2
     |
 public_html

 

Where home is localhost and ex2 is example 2 site.

I then tell apache to use www.ex2.lcl as the web root for this project so when I type in www.ex2.lcl into my web browser it opens d:\home\ex2\public_html

 

home also would be considered htdocs folder or the folder localhost is mapped to.

 

So I can do something like this:

 


              home
       ________|________________________       
       |           |          |        | 
      ex1         ex2       site3    site4
                   |
                   |   
        ___________|_____________
        |           |           |  
    includes   public_html   mysql.inc.php    




 

 

ex2\html would go in public_html folder, mysql.inc.php is inside ex2 folder and my other includes could go in the includes sub folder inside the ex2 folder. So ex2, is outside the web accessible folder of public_html. Though technically localhost in this example can access it but I never use localhost myself so it doesn't matter.

 

In the ZIP file it is layed out like

 

 


              ex2
     __________|______________________________________
     |           |          |         |              |
   private     html      sql.sql  README.txt  mysql.inc.php

 

ex2 is the parent folder that the ex2 web root sits inside of, the above layout this is the html folder.

 

so the example site 2 php files and website files are located in the html folder.

 

mysql.inc.php is the mysql connection file, I put this inside of a conn folder myself but as is, it is sitting outside of the web root so it is ok to leave it there.

 

 

Now to correlate the above to your setup it would be like this:

 

I do not know the actual folders so I am going to guess.

 

 


//localhost/htdocs/cp


    c:\xamp\htdocs\cp


     htdocs   <--  localhost
        |
       cp

 

When you type localhost into your browser it should open htdocs folder. If it opens XAMP then your server is setup wrong, XAMP I assume would be where apache, php, mysql folders are located. localhost should NEVER have access to that folder.

 

 

htdocs woould be in testing environment world document root and also web root. IN LIVE web server, hosted on internet it is setup different where there is a parent folder on top of htdocs that is NOT accessible by web at all. To mimic that, you just add a folder between htdocs and cp then change apache's document root to that second folder, this way you can have multiple sites running off same localhost within same folder structure. Though technically they can be anywhere.

  • Upvote 1
Link to comment
Share on other sites

I edited my posts above a dozen times to make corrections. I am now royally confused about this. LOL

 

I can't say there are no errors or bugs in code samples or what I typed above. It should be enough to start with and help you understand the process.

Please ask questions because I am sort of out of steam on it. LOL

 

In my testing environment I deleted the htdocs folder entirely and placed my website files on a seperate hard drive and then told apache where those are at and what web url to use to access them. You should be able to use htaccess files in your testing environment as well, if not then you need to reconfigure your server software.

 

I also customized a php.ini file in my local sites that puts php errors in the folder the file that generated those errors is located. That is a php configuration not apache, there maybe a way to get apache log files to output in each local sites web root as well but I don't know how to do that and would have to work with special constants which I am not sure apache is setup for, I never looked. To do that with PHP you need to either customize htaccess file OR create a custom php.ini file for each site. I have 30 or so sites setup in my testing enviornment so I can't really use a common config file except for basic default settings, but that is more advanced than I care to go into.

 

 

Thanks.

  • Upvote 1
Link to comment
Share on other sites

I am using wamp and cannot get ex2 to show images.

I created a folder called ex2 and copied the html folder contents into it.

I then move the mysql.inc.php file into the ex2 folder.

 

I think the .htaccess file might be causing the issue for me.

 

Am I doing something wrong?

 

Thanks

 

Frank

Link to comment
Share on other sites

No, it's not the htaccess that's causing the problem, it's the paths to the images in the HTML and CSS. By default (i.e., in the book and the code), the paths start with /, indicating that the images and CSS and such are in the root directory. Since you've put your files in a subdirectory, you need to change references to /something to /ex2/html/something. You'll need to do this to the HTML for the links as well.

Link to comment
Share on other sites

 Share

×
×
  • Create New...