Difference between revisions of "Configuring a LAMPP server for PHP development/Linux desktop"

From Joomla! Documentation

< Configuring a LAMPP server for PHP development
(→‎Adding Yourself to the Apache Group and Modifying Permissions: Corrected user "wwww-data" to www-data. Corrected APahce to Apache.)
(52 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{inuse}}
+
{{underconstruction}}
 
{{RightTOC}}
 
{{RightTOC}}
  
Line 7: Line 7:
  
 
Theses instructions should work fine on any Debian based distribution such as Debian, Ubuntu, LinuxMint, Xubuntu, Kbuntu and others.
 
Theses instructions should work fine on any Debian based distribution such as Debian, Ubuntu, LinuxMint, Xubuntu, Kbuntu and others.
 +
 +
<span style="color:red; font-weight:bold;"> '''NOTE:'''To complement the security of your computer install a firewall to block external incoming traffic to your web service, you may also have to change some directives on your site configuration file to only serve request to the localhost address.</span>
  
 
== Installation ==
 
== Installation ==
'''NOTE:''' You need a stable Internet connection for this tutorial
+
'''NOTE:''' You need a stable Internet connection for this tutorial, if you previously tried to install the LAMP stack and failed for any reason visit this page and follow the instruction here to delete any server configuration and start from scratch "[https://help.ubuntu.com/community/ApacheMySQLPHP#Starting_over:_How_to_remove_the_LAMP_stack How to remove the LAMP stack]"
  
The installation of a LAMPP server on Linux is extremely easy, just follow this instructions:
+
The installation of a LAMPP server on Linux is extremely easy.  Just follow these instructions:
  
 
*Open a terminal and type:
 
*Open a terminal and type:
  
  <tt>sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server phpmyadmin libapache2-mod-suphp</tt>
+
  <tt>sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server phpmyadmin php5-curl</tt>
  
*Say yes [Y] when the package manager ask you download and install the packages, this step will take some time depending on your connection speed
+
*Say yes [Y] when the package manager ask you download and install the packages.  This step will take some time depending on your connection speed.
*At some point the installer will ask you for the MySQL root password use any password you like, but for this example we are going to use "myadmin"
+
*At some point the installer will ask you for the MySQL root password.  Use any password you like.  For this example we are going to use "myadmin".
*The installer will ask for "the web server that should be automatically configured to run phpmyadmin", press [spacebar] to choose "apache2" and press [enter], '''NOTE:''' make sure the selection is marked with and asterisk [*]  
+
*The installer will ask for "the web server that should be automatically configured to run phpmyadmin".  Press [spacebar] to choose "apache2" and press [enter]'''NOTE:''' make sure the selection is marked with and asterisk [*].
*The installer will ask for "Configure database for phpmyadmin with dbconfig-common", choose "<yes>" and press [enter]
+
*The installer will ask for "Configure database for phpmyadmin with dbconfig-common".  Choose "<yes>" and press [enter].
*The installer will ask for "password of the database's administrative user", use any password you like, but for this example we are going to use "myadmin"
+
*The installer will ask for "password of the database's administrative user".  Use any password you like, but for this example we are going to use "myadmin".
*The installer will ask for "mysql application password for phpmyadmin", use any password you like, but for this example we are going to use "myadmin"
+
*The installer will ask for "mysql application password for phpmyadmin".  Use any password you like, but for this example we are going to use "myadmin".
*If no errors have being displayed then the installation is finish
+
*If no errors have being displayed then the installation is finished.
  
=== 1st test for Apache ===
+
=== Apache Installation Test ===
  
 
*Open your web browser and type in the address bar "localhost" and press [enter]
 
*Open your web browser and type in the address bar "localhost" and press [enter]
Line 34: Line 36:
 
  The web server software is running but no content has been added, yet.
 
  The web server software is running but no content has been added, yet.
  
=== 1st test for PHP server ===
+
=== PHP Installation Test ===
  
To test if PHP server is working lets create a quick test file using the command line
+
To test if PHP server is working lets create a quick test file using the command line.
  
 
*Open a terminal and type
 
*Open a terminal and type
Line 43: Line 45:
  
 
*Open your web browser and type in the address bar "localhost/test.php" and press [enter]
 
*Open your web browser and type in the address bar "localhost/test.php" and press [enter]
*The next thing you should see in your browser is a really long page displaying information about the PHP server, if not then something went wrong
+
*The next thing you should see in your browser is a really long page displaying information about the PHP server, if not, the installation was not successful.
*Now that we know the PHP server is working fine we don't need that test file anymore, type the following command in your terminal to delete the file
+
*Once the PHP server is working fine we don't need that test file anymore, so type the following command in your terminal to delete the file:
  
 
  <tt>sudo rm /var/www/test.php</tt>
 
  <tt>sudo rm /var/www/test.php</tt>
  
=== 1st test for phpMyAdmin ===
+
=== phpMyAdmin Installation Test ===
  
*Open your web browser and type in the address bar "localhost/phpmyadmin" and press [enter]
+
*Open your web browser and type in the address bar "localhost/phpmyadmin" and press [enter].
*The next thing you should see is the phpMyadmin login page, if not then something went wrong, most likely you skip or not marked the option "apache2" at the question "web server that should be automatically configured to run phpmyadmin", to fix this problem just purge the installation and start over again the installation steps
+
*The next thing you should see is the phpMyadmin login page.  If not then most likely you skipped or did not mark the option "apache2" at the question "web server that should be automatically configured to run phpmyadmin".  To fix this problem just purge the installation and start over again.
 
*Login to phpmyadmin with the following credentials  
 
*Login to phpmyadmin with the following credentials  
 
**username = root
 
**username = root
 
**password = myadmin
 
**password = myadmin
*You should be able to login normally and have no error messages at all
+
*You should be able to login normally and have no error messages at all.
  
== Understanding the folder structure ==
+
== Understanding the Folder Structure ==
  
There are several folders and files that the LAMP server uses to store the configurations of the LAMP services and to store the files of your hosted websites
+
There are several folders and files that the LAMP server uses to store the configurations of the LAMP services and to store the files of your hosted websites.
  
=== Apache default web site folder ===  
+
=== Apache Default Web Site Folder ===  
  
 
Location: "/var/www/"
 
Location: "/var/www/"
  
Description: by default the Apache server enables a test website and store the website files in that location, so every time you visit the page local host, the browser display the html page located there..
+
Description: by default the Apache server enables a test website and store the website files in that location.  So every time you visit the page '''<nowiki>http://localhost</nowiki>''', the browser displays the page located at "/var/www/".
  
With your file browser navigate to "/var/www/" there should be a file called "index.html", change the content of the file for whatever you want and refresh the web page to see the changes.  
+
With your file browser navigate to "/var/www/".  There should be a file called "index.html".  Change the content of the file for whatever you want and refresh the web page to see the changes.
  
=== Apache web sites configuration files ===  
+
=== Apache Web Sites Configuration Files ===  
  
 
Location: "/etc/apache2/sites-available/"
 
Location: "/etc/apache2/sites-available/"
  
Description: You can host multiples sites in the same server, this folder a configuration file for each site.
+
Description: You can host multiples sites in the same server.  This folder a configuration file for each site.
  
=== Apache configuration file ===  
+
=== Apache Configuration Files ===  
  
 
Location: "/etc/apache2/apache2.conf"
 
Location: "/etc/apache2/apache2.conf"
 
Location: "/etc/apache2/envvars"
 
Location: "/etc/apache2/envvars"
  
Description: This files contains very important information about the Apache service.
+
Description: These files contain very important information about the Apache service.
  
=== Apache ports configuration file ===  
+
=== Apache Ports Configuration File ===  
  
 
Location: "/etc/apache2/ports.conf"
 
Location: "/etc/apache2/ports.conf"
  
Description: This files configure what port will Apache server listen to for http requests, by default http request are assigned to the port 80 but you can modify or add more ports.
+
Description: This file configures what port the Apache server will listen to for http requests.  By default, http requests are assigned to port 80 but you can modify or add more ports.
  
=== Apache log files ===  
+
=== Apache Log Files ===  
  
 
Location: "/var/log/apache2/"
 
Location: "/var/log/apache2/"
  
Description: That folder contain several files to keep track of several events on your Apache web server, such as errors in the services, errors in code of your site, failed authentication attempts and more, this is a good place to look at when something is not working file or you suspect some is trying to breach your server security
+
Description: This folder contains several files to keep track of events on your Apache web server, such as errors in the services, errors in code of your site, failed authentication attempts and more.  This is a good place to look at when something is not working file or you suspect some is trying to breach your server security.
 +
 
  
 
== Configuration ==
 
== Configuration ==
  
=== Deploying a new site location ===
+
=== Enabling mod_rewrite ===
 +
 
 +
The mod_rewrite module uses a rule-based rewriting engine, based on a PCRE regular-expression parser to rewrite requested URLs on the fly. By default, mod_rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL, or to invoke an internal proxy fetch.
 +
 
 +
For more information, visit http://httpd.apache.org/docs/current/mod/mod_rewrite.html
 +
 
 +
* Open a terminal and type:
 +
 
 +
<tt>sudo a2enmod rewrite</tt>
 +
 
 +
* Now that the rewrite module is enabled, we need to restart apache.
 +
 
 +
<tt>sudo service apache2 restart</tt>
  
By default the web server is hosting the files in the location "/var/www" but for security reason and for the sake of avoid ownership problems we are going to use another place to host our web site files
+
* done
  
Lets create a new folder to store the web files and the log files of the server
+
=== Deploying a New Site Folder Structure ===
 +
By default the Web server is hosting the files in the location "/var/www", but for security reasons and to avoid ownership problems, we are going to use another place to host our Website files.
  
* open a terminal and type
+
Let's create a new folder to store the Web files and the log files of the site.
  
<tt>mkdir /home/youruser/lamp/
+
Open a terminal and type:
 +
<tt>
 +
mkdir /home/youruser/lamp/
 
  mkdir /home/youruser/lamp/public_html/
 
  mkdir /home/youruser/lamp/public_html/
  mkdir /home/youruser/lamp/logs/</tt>
+
  mkdir /home/youruser/lamp/logs/
 +
</tt>
  
'''NOTE:''' You can place your new site folders on any location you desire, this is just an example, replace "youruser" with your actual Linux username
+
'''NOTE:''' You can place your new site folders at any location you desire; this is just an example. Replace "youruser" with an actual Linux username.
  
To store the web site files we are going to use the folder "plublic_html" and for our log files we are going to use the folder "logs"
+
To store the Web site files we are going to use the folder "public_html". For our log files, we are going to use the folder "logs".
  
=== Creating the new site ===
+
=== Creating the New Site ===
  
To create and enable a new site in your server follow this steps:
+
To create and enable a new site in your server follow these steps:
  
 
'''NOTE:''' gedit is a common Linux editor but you can use any other alternative you like such as geany, nano, vim, pico, etc...
 
'''NOTE:''' gedit is a common Linux editor but you can use any other alternative you like such as geany, nano, vim, pico, etc...
  
*open a terminal an type
+
*Open a terminal an type:
  
  <tt>cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mydevsite</tt>
+
  <tt>sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mydevsite</tt>
  
'''NOTE:''' "mydevsite" is the name of the new site used in this example, you can use any other name you like
+
'''NOTE:''' "mydevsite" is the name of the new site used in this example.  You can use any other name you like.
  
 
*Open the site configuration
 
*Open the site configuration
Line 128: Line 147:
 
  <tt>sudo gedit /etc/apache2/sites-available/mydevsite</tt>
 
  <tt>sudo gedit /etc/apache2/sites-available/mydevsite</tt>
  
*The content of that file should be something like this
+
*The content of that file should be something like this:
  
 
<tt>
 
<tt>
Line 177: Line 196:
  
 
<tt>
 
<tt>
  <VirtualHost *:80 *:8080>
+
  <VirtualHost *:80>
 
  ServerAdmin webmaster@localhost
 
  ServerAdmin webmaster@localhost
 
   
 
   
Line 220: Line 239:
 
</tt>
 
</tt>
  
'''NOTE:''' Replace "yourname" with your current user name
+
'''NOTE:''' Replace "youruser" with your current user name.
  
*Save changes
+
*Save your changes.
*No we need to enable the site, in a terminal type  
+
*Now we need to enable the site.  In a terminal type:
  
 
  <tt>sudo a2ensite mydevsite</tt>
 
  <tt>sudo a2ensite mydevsite</tt>
  
*Lets disable the default site, we don't need it anymore
+
*Lets disable the default site, since we don't need it anymore.
  
 
  <tt>sudo a2dissite default</tt>
 
  <tt>sudo a2dissite default</tt>
  
*Restart Apache to complete the process, in a terminal type
+
*Restart Apache to complete the process.  In a terminal type:
 
   
 
   
 
  <tt>sudo service apache2 restart</tt>
 
  <tt>sudo service apache2 restart</tt>
  
*To test out our new site lets create a quick test file, in a terminal type
+
*To test out our new site, lets create a quick test file.  In a terminal type:
  
  <tt>echo "<?php echo 'Hello world, today is is: '.echo date('Y/m/d'); ?>" | tee /home/youruser/lamp/public_html/today.php </tt>
+
  <tt>echo "<?php echo 'Hello world, today is is: '.date('Y/m/d'); ?>" | tee /home/youruser/lamp/public_html/today.php </tt>
  
'''NOTE:''' Replace "yourname" with your current user name
+
'''NOTE:''' Replace "yourname" with your current user name.
  
*Open your browser an navigate to "localhost/today.php"
+
*Open your browser an navigate to "localhost/today.php".
*If everything is working ok you should see something like this  
+
*If everything is working ok you should see something like this:
  
 
  <tt>Hello world, today is is: 2012/05/05</tt>
 
  <tt>Hello world, today is is: 2012/05/05</tt>
  
=== Preventing ownership problems ===
+
=== Enabling Additional Ports ===
 +
 
 +
Note: If you have no plans to show your local site to another person over the Internet just skip this section.
 +
 
 +
With the last configuration you should be able to access your page and access it from another computer connected to your LAN.  If your computer is connected to Internet and also has an assigned Public IP you can access your site using that IP from any web browser, but for some reason some ISPs does not allow HTTP traffic (HTTP = port 80) over dynamic IPs.  To solve this you just need to configure Apache to reply requests from a different port.  In this case we are going to use the port number 8080 which is easy to remember.
 +
 
 +
If you are using a router to connect to Internet you should have to configure a port forwarding setting on your router to let other people see your local site.  Google "how to do port forwarding" on your current router model. If you don't now what is the difference between a Static IP, Dynamic IP,  Private IP and a Public IP we recommend you to do a Wikipedia reading about these topics.
 +
 
 +
*Open a terminal and type:
 +
 
 +
<tt>sudo gedit /etc/apache2/ports.conf</tt>
 +
 
 +
*Find the line "listen 80" and insert this line underneath:
 +
 
 +
<tt>Listen 8080</tt>
 +
 
 +
*Save changes.
  
By default in some Linux installations the Apache server runs under the user "www-data" which is also in the "www-data" group, this behavior will bring us problems in the future because any file modified or created by the server will have a different ownership, in other words you wouldn't be able to edit some files created or modified by the server unless you manually change the permissions of each file to something like 777 or execute your editor as "super user" which both are really bad ideas.
+
*Open your new site configuration.
  
==== Method 1: Implementing suPHP ====
+
<tt>sudo gedit /etc/apache2/sites-available/mydevsite</tt>
  
suPHP is an Apache module used to execute PHP scripts with the permissions of their file owners
+
*Find this directive "<VirtualHost *:80>" and make the following modification.
  
This is how the server will work thanks to suPHP
+
<tt> <VirtualHost *:80 *:8080> </tt>
  
*If a PHP file have the owner "dexter" suPHP will execute that file as "dexter" and not as the Apache user aka "www-data",
+
*Save changes.
*If another file PHP file have the owner "adam" suPHP will execute that file as "adam" and not as the Apache user aka "www-data"
 
*If another file PHP file have the owner "www-data" suPHP will execute that file as "www-data" which is the Apache user
 
*If a folder have the owner "dexter" and it have a PHP file inside it with the owner "adam" the server will throw a "500" error when some one tries to request that file because it does not belong to "dexter"
 
*If a any PHP script tries to read or write files or folders outside the server's document root, then the server will deny the action
 
*If a file have too permissive permissions such as "chmod 666", then the server will throw a "500" error because suPHP don't allow too permissive permissions for security reasons
 
  
We already have suPHP installed, to Configure it follow this steps:
+
*Restart Apache to complete the process.  In a terminal type:
  
*Open a terminal and Type
+
<tt>sudo service apache2 restart</tt>
  
  <tt>sudo gedit /etc/suphp/suphp.conf</tt>
+
*To test your new configuration, try to access your site from another computer over Internet. Just type your IP in the browser's address bar and press enter.  If the request fails, try the new alternative port like this: xxx.xxx.xxx.xxx:8080
  
*Open a terminal and Type
+
=== Preventing Ownership and Permissions Problems ===
  
  <tt>sudo gedit /etc/suphp/suphp.conf</tt>
+
On Linux machines, file permissions are an important thing. Linux uses a mechanism to control what users can do and cannot do about folders, files and even the execution of applications. This mechanism consists of parameters, the ownership and the permissions.
  
*Find the option "docroot" and set the location of your public_html folder, like this
+
==== Files and Folders Ownership ====
  
  <tt>docroot= /home/youruser/lamp/public_html</tt>
+
Ownership have two parameters: The owner and group.
  
'''NOTE:''' You can place your new site folders on any location you desire, this is just an example, replace "youruser" with your actual Linux username
+
The "owner" is who owns the file or folder and is represented by a "user name".  In Linux persons, applications and services use usernames.  On most Linux distributions, the Apache service runs under the user name "www-data".
  
*Save changes
+
The "group" is used to associate users into an logical group.  This figure is useful when an administrator needs to grant or deny permissions to several users with one single command and not user by user.
*Type in your terminal
 
  
<tt>sudo gedit /etc/apache2/mods-available/php5.conf</tt>
+
==== Files and Folders Permissions ====
  
*On your editor create a new empty line at the first line of the document and add this text there
+
The permissions have three parameters that represent file and folder permissions for the owner, the group and others. These number range from 0 to 7, and mean the following:
  
<tt><Directory /usr/share></tt>
+
* 4 = permission to read
 +
* 2 = permission to write
 +
* 1 = permission to execute
 +
* 0 = no permissions at all
  
*Then at the end of the document create another empty line and add this text there
+
Note: "Others" represents everybody.  This parameter is used to grant permission to everyone no matter the user or the group they belong to. This parameter should be set with care under certain situations.  Granting incorrect permissions to sensitive files and folders can cause security problems.
  
<tt></Directory></tt>
+
For example if we got a file with permissions like this "644", it means (owner=read+write)(group=read)(others=read).
  
*Save changes
+
To get more information about the Linux file systems and file permissions read [http://docs.joomla.org/How_do_UNIX_file_permissions_work%3F this] article.
*Type in your terminal
 
  
<tt>sudo service apache2 restart</tt>
+
==== Adding Yourself to the Apache Group and Modifying Permissions ====
  
*Now suPHP should be up and running, now we need to adjust the proper file ownership to the server document root
+
For this example, your username will be "youruser". On most Linux distributions, the Apache service runs on the user "www-data" and the group "www-data". We need to include our user "youruser" in the "www-data" group to be able to set permissions to the Web server files and have no problems when we have to edit them.
*Type in your terminal
 
  
<tt>sudo find . -type f -exec chmod 644 {} \;
+
*To add "youruser" to the Apache group, open a terminal and type:
sudo find . -type d -exec chmod 755 {} \;
 
sudo chown youruser:youruser -R /home/youruser/lamp/public_html/</tt>
 
  
'''NOTE:''' You can place your new site folders on any location you desire, this is just an example, replace "youruser" with your actual Linux username
+
<tt>sudo adduser youruser www-data</tt>
  
*Lets create a file to do a quick test to see if suPHP is working correctly, type in your terminal
+
*Now we need to change the owner and group of all our Web server files to owner "www-data" and group "www-data".
  
  <tt>echo "<?php echo 'whoim = '.exec('/usr/bin/whoami');?>" | tee /home/myadmin/lamp/public_html/whomi.php
+
  <tt>sudo chown -R www-data:www-data /home/youruser/lamp/public_html</tt>
chmod 644 /home/myadmin/lamp/public_html/whomi.php</tt>
 
  
*Open your browser and navigate to "localhost/whomi.php", you should see something like this
+
*Finally we have to set the correct folder permission so both Apache and our user can edit the files with no problems. On a terminal type:
  
  whomi = youruser
+
  <tt>sudo chmod -R 775 /home/youruser/lamp/public_htm</tt>
  
That means the script is being executed with your user and not the Apache user
+
==== Common Problems and Confusions ====
  
==== Method 2: Changing Apache user and group ====
+
When Joomla creates files on extension installations or any other operation, it uses the default mask 755 for folders and 644 for files.  These permissions are correct and secure for production servers but it will give us problems on our local server because our editor will not be able to edit those files and folders.  To fix these problems, simply run again these two commands:
  
'''NOTE:'''This method is highly discouraged, do not implement in a computer with personal or sensitive information, install a firewall to block external incoming traffic to your web server.
+
<tt>sudo chmod -R 775 /home/youruser/lamp/public_htm</tt>
  
To make Apache execute under your current user and group you got to edit some parameters in the Apache configuration file and make it execute under our current user and group, this will solve our file ownership problems '''but opens a severe security hole'''.
+
In the case you manually move files from your personal folders to the server web folder "public_html" those new files most likely will be owned by your username.  This can cause problems if the server needs to modify or delete information.  To prevent this problem, every time you move or copy information to your server web folder you have to set the correct owner and group permissions for all those new files.  Open a terminal and run this command:
  
To change the user and group of the Apache service, follow these instructions:
+
<tt>sudo chown -R www-data:www-data /home/youruser/lamp/public_html</tt>
  
*open a terminal and type
+
Note: If your server has too many files, these commands could be slow but you can always set a more specific path to apply permission in less files.
  
<tt>sudo gedit /etc/apache2/envvars</tt>
+
=== Other Configurations ===
  
*Find the lines
+
*Open a terminal and type:
  
  <tt>export APACHE_RUN_USER=www-data
+
  <tt> sudo gedit /etc/php5/apache2/php.ini </tt>
export APACHE_RUN_GROUP=www-data</tt>
 
  
*Replace the "www-data" with your current username in both lines
+
*Find the line:
*Save changes
 
*Type in your terminal
 
  
  <tt>sudo service apache2 restart</tt>
+
  <tt>output_buffering =</tt>
  
*Done
+
*Set the value to "= Off".
  
 +
*Find the line:
  
 +
<tt>post_max_size =</tt>
  
 +
*Set the value to "= 20M".
  
 +
*Find the line:
  
 +
<tt>upload_max_filesize =</tt>
  
 +
*Set the value to "= 20M".
  
 +
* Save changes.
  
 +
*Type in your terminal:
  
 +
<tt>sudo service apache2 restart</tt>
  
 +
=== Enforcing Security ===
 +
Since your computer is now running Web services, these services are listening for requests and will reply to anyone who has the correct IP and port. In other words, other people in your LAN and Internet can access your local site without your permission or they can even try to crack/hack your workstation. To prevent this, you need to install a firewall and "deny" by default any external incoming requests to your computer.
  
 +
For Linux users there is a nice and simple firewall called "Uncomplicated Firewall". To install the user interface and manage the firewall from your desktop, just follow these steps:
  
 +
*Open a terminal and type:
  
 +
<tt>sudo apt-get install gufw</tt>
  
 +
Note: You can also install the application from the Software Manager.
  
To complete the installation of our web server we need to apply several configuration changes, first of all lets stop the LAMP services. Open a terminal a type this:
+
*Open the application. When the installation finishes, press the "unlock" button and type your administrative password.
  
service apache2 stop
+
*Set "Status = On" and "Incoming = Deny". Leave the rest in their default values.
  
If you open your web browser and navigate to "localhost" you should get an error such as "unable to connect" or "could not connect to localhost"
+
*To test your firewall, just try to connect to your local site from a local computer on your LAN or a remote computer over the Internet. When the firewall status is "Status = On", you shouldn't be able to connect at all.
  
== Fine tuning ==
+
*Now ''temporarily'' change the status of your firewall to "Status = Off" and try to connect again. People should be able to see your local site just fine. Remember to set "Status = On" after this test.
  
 +
Note: In this tutorial we are denying any incoming external requests to any port. As a side note, you can also Deny all incoming requests and manually allow incoming requests to few specific ports if you wish, but those kinds of settings are up to you. Since a PC workstation is not a server, it is okay to deny all incoming traffic by default.
  
[[Category:Development]]
+
[[Category:Server configurations]]

Revision as of 15:48, 20 September 2014

Documentation all together tranparent small.png
Under Construction

This article or section is in the process of an expansion or major restructuring. You are welcome to assist in its construction by editing it as well. If this article or section has not been edited in several days, please remove this template.
This article was last edited by Cmb (talk| contribs) 9 years ago. (Purge)

Introduction[edit]

This article provides detailed instructions for configuring a LAMPP server, not only for Joomla! it also should work fine for PHP development in general.

Theses instructions should work fine on any Debian based distribution such as Debian, Ubuntu, LinuxMint, Xubuntu, Kbuntu and others.

NOTE:To complement the security of your computer install a firewall to block external incoming traffic to your web service, you may also have to change some directives on your site configuration file to only serve request to the localhost address.

Installation[edit]

NOTE: You need a stable Internet connection for this tutorial, if you previously tried to install the LAMP stack and failed for any reason visit this page and follow the instruction here to delete any server configuration and start from scratch "How to remove the LAMP stack"

The installation of a LAMPP server on Linux is extremely easy. Just follow these instructions:

  • Open a terminal and type:
sudo apt-get install apache2 php5-mysql libapache2-mod-php5 mysql-server phpmyadmin php5-curl
  • Say yes [Y] when the package manager ask you download and install the packages. This step will take some time depending on your connection speed.
  • At some point the installer will ask you for the MySQL root password. Use any password you like. For this example we are going to use "myadmin".
  • The installer will ask for "the web server that should be automatically configured to run phpmyadmin". Press [spacebar] to choose "apache2" and press [enter]. NOTE: make sure the selection is marked with and asterisk [*].
  • The installer will ask for "Configure database for phpmyadmin with dbconfig-common". Choose "<yes>" and press [enter].
  • The installer will ask for "password of the database's administrative user". Use any password you like, but for this example we are going to use "myadmin".
  • The installer will ask for "mysql application password for phpmyadmin". Use any password you like, but for this example we are going to use "myadmin".
  • If no errors have being displayed then the installation is finished.

Apache Installation Test[edit]

  • Open your web browser and type in the address bar "localhost" and press [enter]
  • Normally Apache display a test page with some text like this:
It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.

PHP Installation Test[edit]

To test if PHP server is working lets create a quick test file using the command line.

  • Open a terminal and type
echo "<?php phpinfo(); ?>" | sudo tee /var/www/test.php 
  • Open your web browser and type in the address bar "localhost/test.php" and press [enter]
  • The next thing you should see in your browser is a really long page displaying information about the PHP server, if not, the installation was not successful.
  • Once the PHP server is working fine we don't need that test file anymore, so type the following command in your terminal to delete the file:
sudo rm /var/www/test.php

phpMyAdmin Installation Test[edit]

  • Open your web browser and type in the address bar "localhost/phpmyadmin" and press [enter].
  • The next thing you should see is the phpMyadmin login page. If not then most likely you skipped or did not mark the option "apache2" at the question "web server that should be automatically configured to run phpmyadmin". To fix this problem just purge the installation and start over again.
  • Login to phpmyadmin with the following credentials
    • username = root
    • password = myadmin
  • You should be able to login normally and have no error messages at all.

Understanding the Folder Structure[edit]

There are several folders and files that the LAMP server uses to store the configurations of the LAMP services and to store the files of your hosted websites.

Apache Default Web Site Folder[edit]

Location: "/var/www/"

Description: by default the Apache server enables a test website and store the website files in that location. So every time you visit the page http://localhost, the browser displays the page located at "/var/www/".

With your file browser navigate to "/var/www/". There should be a file called "index.html". Change the content of the file for whatever you want and refresh the web page to see the changes.

Apache Web Sites Configuration Files[edit]

Location: "/etc/apache2/sites-available/"

Description: You can host multiples sites in the same server. This folder a configuration file for each site.

Apache Configuration Files[edit]

Location: "/etc/apache2/apache2.conf" Location: "/etc/apache2/envvars"

Description: These files contain very important information about the Apache service.

Apache Ports Configuration File[edit]

Location: "/etc/apache2/ports.conf"

Description: This file configures what port the Apache server will listen to for http requests. By default, http requests are assigned to port 80 but you can modify or add more ports.

Apache Log Files[edit]

Location: "/var/log/apache2/"

Description: This folder contains several files to keep track of events on your Apache web server, such as errors in the services, errors in code of your site, failed authentication attempts and more. This is a good place to look at when something is not working file or you suspect some is trying to breach your server security.


Configuration[edit]

Enabling mod_rewrite[edit]

The mod_rewrite module uses a rule-based rewriting engine, based on a PCRE regular-expression parser to rewrite requested URLs on the fly. By default, mod_rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL, or to invoke an internal proxy fetch.

For more information, visit http://httpd.apache.org/docs/current/mod/mod_rewrite.html

  • Open a terminal and type:
sudo a2enmod rewrite
  • Now that the rewrite module is enabled, we need to restart apache.
sudo service apache2 restart
  • done

Deploying a New Site Folder Structure[edit]

By default the Web server is hosting the files in the location "/var/www", but for security reasons and to avoid ownership problems, we are going to use another place to host our Website files.

Let's create a new folder to store the Web files and the log files of the site.

Open a terminal and type:

mkdir /home/youruser/lamp/
mkdir /home/youruser/lamp/public_html/
mkdir /home/youruser/lamp/logs/

NOTE: You can place your new site folders at any location you desire; this is just an example. Replace "youruser" with an actual Linux username.

To store the Web site files we are going to use the folder "public_html". For our log files, we are going to use the folder "logs".

Creating the New Site[edit]

To create and enable a new site in your server follow these steps:

NOTE: gedit is a common Linux editor but you can use any other alternative you like such as geany, nano, vim, pico, etc...

  • Open a terminal an type:
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mydevsite

NOTE: "mydevsite" is the name of the new site used in this example. You can use any other name you like.

  • Open the site configuration
sudo gedit /etc/apache2/sites-available/mydevsite
  • The content of that file should be something like this:

<VirtualHost *:80>
	ServerAdmin webmaster@localhost

	DocumentRoot /var/www
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /var/www/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

  • Make some modifications to make it looks like this, or simply copy and paste it

<VirtualHost *:80>
	ServerAdmin webmaster@localhost

	DocumentRoot /home/youruser/lamp/public_html
	<Directory />
		Options FollowSymLinks
		AllowOverride All
	</Directory>
	<Directory /home/youruser/lamp/public_html>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride All
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog /home/youruser/lamp/logs/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog /home/youruser/lamp/logs/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride All
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

NOTE: Replace "youruser" with your current user name.

  • Save your changes.
  • Now we need to enable the site. In a terminal type:
sudo a2ensite mydevsite
  • Lets disable the default site, since we don't need it anymore.
sudo a2dissite default
  • Restart Apache to complete the process. In a terminal type:
sudo service apache2 restart
  • To test out our new site, lets create a quick test file. In a terminal type:
echo "<?php echo 'Hello world, today is is: '.date('Y/m/d'); ?>" | tee /home/youruser/lamp/public_html/today.php 

NOTE: Replace "yourname" with your current user name.

  • Open your browser an navigate to "localhost/today.php".
  • If everything is working ok you should see something like this:
Hello world, today is is: 2012/05/05

Enabling Additional Ports[edit]

Note: If you have no plans to show your local site to another person over the Internet just skip this section.

With the last configuration you should be able to access your page and access it from another computer connected to your LAN. If your computer is connected to Internet and also has an assigned Public IP you can access your site using that IP from any web browser, but for some reason some ISPs does not allow HTTP traffic (HTTP = port 80) over dynamic IPs. To solve this you just need to configure Apache to reply requests from a different port. In this case we are going to use the port number 8080 which is easy to remember.

If you are using a router to connect to Internet you should have to configure a port forwarding setting on your router to let other people see your local site. Google "how to do port forwarding" on your current router model. If you don't now what is the difference between a Static IP, Dynamic IP, Private IP and a Public IP we recommend you to do a Wikipedia reading about these topics.

  • Open a terminal and type:
sudo gedit /etc/apache2/ports.conf
  • Find the line "listen 80" and insert this line underneath:
Listen 8080
  • Save changes.
  • Open your new site configuration.
sudo gedit /etc/apache2/sites-available/mydevsite
  • Find this directive "<VirtualHost *:80>" and make the following modification.
 <VirtualHost *:80 *:8080> 
  • Save changes.
  • Restart Apache to complete the process. In a terminal type:
sudo service apache2 restart
  • To test your new configuration, try to access your site from another computer over Internet. Just type your IP in the browser's address bar and press enter. If the request fails, try the new alternative port like this: xxx.xxx.xxx.xxx:8080

Preventing Ownership and Permissions Problems[edit]

On Linux machines, file permissions are an important thing. Linux uses a mechanism to control what users can do and cannot do about folders, files and even the execution of applications. This mechanism consists of parameters, the ownership and the permissions.

Files and Folders Ownership[edit]

Ownership have two parameters: The owner and group.

The "owner" is who owns the file or folder and is represented by a "user name". In Linux persons, applications and services use usernames. On most Linux distributions, the Apache service runs under the user name "www-data".

The "group" is used to associate users into an logical group. This figure is useful when an administrator needs to grant or deny permissions to several users with one single command and not user by user.

Files and Folders Permissions[edit]

The permissions have three parameters that represent file and folder permissions for the owner, the group and others. These number range from 0 to 7, and mean the following:

  • 4 = permission to read
  • 2 = permission to write
  • 1 = permission to execute
  • 0 = no permissions at all

Note: "Others" represents everybody. This parameter is used to grant permission to everyone no matter the user or the group they belong to. This parameter should be set with care under certain situations. Granting incorrect permissions to sensitive files and folders can cause security problems.

For example if we got a file with permissions like this "644", it means (owner=read+write)(group=read)(others=read).

To get more information about the Linux file systems and file permissions read this article.

Adding Yourself to the Apache Group and Modifying Permissions[edit]

For this example, your username will be "youruser". On most Linux distributions, the Apache service runs on the user "www-data" and the group "www-data". We need to include our user "youruser" in the "www-data" group to be able to set permissions to the Web server files and have no problems when we have to edit them.

  • To add "youruser" to the Apache group, open a terminal and type:
sudo adduser youruser www-data
  • Now we need to change the owner and group of all our Web server files to owner "www-data" and group "www-data".
sudo chown -R www-data:www-data /home/youruser/lamp/public_html
  • Finally we have to set the correct folder permission so both Apache and our user can edit the files with no problems. On a terminal type:
sudo chmod -R 775 /home/youruser/lamp/public_htm

Common Problems and Confusions[edit]

When Joomla creates files on extension installations or any other operation, it uses the default mask 755 for folders and 644 for files. These permissions are correct and secure for production servers but it will give us problems on our local server because our editor will not be able to edit those files and folders. To fix these problems, simply run again these two commands:

sudo chmod -R 775 /home/youruser/lamp/public_htm

In the case you manually move files from your personal folders to the server web folder "public_html" those new files most likely will be owned by your username. This can cause problems if the server needs to modify or delete information. To prevent this problem, every time you move or copy information to your server web folder you have to set the correct owner and group permissions for all those new files. Open a terminal and run this command:

sudo chown -R www-data:www-data /home/youruser/lamp/public_html

Note: If your server has too many files, these commands could be slow but you can always set a more specific path to apply permission in less files.

Other Configurations[edit]

  • Open a terminal and type:
 sudo gedit /etc/php5/apache2/php.ini 
  • Find the line:
output_buffering =
  • Set the value to "= Off".
  • Find the line:
post_max_size =
  • Set the value to "= 20M".
  • Find the line:
upload_max_filesize =
  • Set the value to "= 20M".
  • Save changes.
  • Type in your terminal:
sudo service apache2 restart

Enforcing Security[edit]

Since your computer is now running Web services, these services are listening for requests and will reply to anyone who has the correct IP and port. In other words, other people in your LAN and Internet can access your local site without your permission or they can even try to crack/hack your workstation. To prevent this, you need to install a firewall and "deny" by default any external incoming requests to your computer.

For Linux users there is a nice and simple firewall called "Uncomplicated Firewall". To install the user interface and manage the firewall from your desktop, just follow these steps:

  • Open a terminal and type:
sudo apt-get install gufw

Note: You can also install the application from the Software Manager.

  • Open the application. When the installation finishes, press the "unlock" button and type your administrative password.
  • Set "Status = On" and "Incoming = Deny". Leave the rest in their default values.
  • To test your firewall, just try to connect to your local site from a local computer on your LAN or a remote computer over the Internet. When the firewall status is "Status = On", you shouldn't be able to connect at all.
  • Now temporarily change the status of your firewall to "Status = Off" and try to connect again. People should be able to see your local site just fine. Remember to set "Status = On" after this test.

Note: In this tutorial we are denying any incoming external requests to any port. As a side note, you can also Deny all incoming requests and manually allow incoming requests to few specific ports if you wish, but those kinds of settings are up to you. Since a PC workstation is not a server, it is okay to deny all incoming traffic by default.