Friday 22 May 2015

Ubuntu Netbeans and LAMP server

In this article the author will show you how to setup Linux Apache Mysql PHP (LAMP) web development environment on Ubuntu  with your web site files in your home directory. This way you can easily develop for web.

The goal of the author in this  session are to install Apache, MySQL and PHP with Xdebug module for PHP development debugging.  The author will setup Xdebug and use it with Netbeans, but once Xdebug is properly installed, you can debug your applications with any other application like Eclipse. The most important thing for a web developer or web programmer is to be able to easily modify his web development files without becoming root for everything he is working on. The author  will show you how to instruct Apache to make virtual host in your home directory. At the end of it all the author will install Netbeans and create sample project to work with our new development environment. So lets get started...
First thing you need to do is to install Linux Apache Mysql PHP server. In Debian based Linux distribution this is easily done using tasksel to select all necessary packages for typical Linux Apache Mysql PHP server. So lets install tasksel program first. Type this at your terminal:

sudo apt-get install tasksel

Next thing is to use tasksel to install Linux Apache Mysql PHP server. We do this this way:

sudo tasksel install lamp-server

In the middle of Linux Apache Mysql PHP installation, tasksel will ask you for MySQL root password. It is very important to write down what you gave to tasksel, because this password will be used to access MySQL database. After Linux Apache Mysql PHP installation is over, you will install Xdebug and one of the PHP image manipulation modules any web developer will need in his work, namely GD library. While you're working with apt-get, you will install Netbeans from Ubuntu repository. Here's the command to do this:

sudo apt-get install php5-xdebug php5-gd netbeans

Author usually use directory "public_html" inside auhtor home to keep all of author web sites code. You can use whatever directory inside you home you want. Important thing to mention is that Ubuntu web server is started as user "www-data" and directory where you keep your web source code should be accessible and writable by this user. So you will designate "www-data" as the owner of "public_html" directory. What's important for you is that you, as users have same permissions like your web servers "www-data" user for "public_html" directory so you could freely do your work. you will accomplish this by adding your selves to the "www-data" group. Now lets create "public_html" directory, adjust its permissions and add your selves to the "www-data" group:

mkdir /home/$USER/public_html 

sudo chown -R www-data:www-data /home/$USER/public_html sudo chmod -R 775 /home/$USER/public_html 

sudo adduser $USER www-data

Now after we have our "public_html" folder in place, we will adjust Apache setting so that "public_html" is used as web server document root. This way when we go to "localhost" or "127.0.0.1" server serves files from "/home/$USER/public_html". We will copy default web site template and modify it:

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/public_html

Now we need to edit new web site template to point to "/home/$USER/public_html" instead of default "/var/www". Lets open our new template for editing:

gksudo gedit /etc/apache2/sites-available/public_html

We need to modify two lines: "DocumentRoot" from "/var/www" to "/home/username/public_html" and location of the second "Directory" section from "/var/www/" to "/home/username/public_html/". Notice the extra "/" on the "Directory" definition when compared to "DocumentRoot", this is on purpose. Instead of "usensame" put you own username. Everything else should be left as it were.
Here's the example of our modified file on my Ubuntu PC. Instead of "Aryan" in this file you must put your own username:
 
<VirtualHost *:80>
 ServerAdmin webmaster@localhost
 
 DocumentRoot /home/Aryan/public_html
 <Directory />
  Options FollowSymLinks
  AllowOverride None
 </Directory>
 <Directory /home/Aryan/public_html/>
  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 /home/Aryan/public_html/error.log
 
 # Possible values include: debug, info, notice, warn, error, crit,
 # alert, emerg.
 LogLevel warn
 
 CustomLog /home/Aryan/public_html/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>
 
Now we can disable Ubuntu default "/var/www"site and enable our new "public_html" site:

sudo a2dissite default sudo a2ensite public_html

What's left is to adjust Xdebug settings to allow debugging. We need to append to Xdebug configuration file:
For older Ubuntu (Debian) versions:

gksudo gedit /etc/php5/conf.d/xdebug.ini

For recent Ubuntu (Debian) versions:

gksudo gedit /etc/php5/mods-available/xdebug.ini

We will add this at the bottom of "/etc/php5/conf.d/xdebug.ini":
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
HERE's the example of old Xdebug configuration file, and HERE's the example of our modified file.
Now we restart Apache server to apply our new settings:

sudo service apache2 restart

The last thing to do is to setup Netbeans with project inside our new "public_html" directory. Because we added our selves to the "www-data" user group, we should restart our PC now to make sure everything is refreshed properly. Next we start Netbeans from Applications -> Programming -> Netbeans and create new project by going to the File -> New Project. In the dialog we select PHP from the left side and "PHP Application" from the right side.
On the next screen you should configure things like this if you want your project to be called "techytalk":
Netbeans New PHP Project - selecting name and location
Netbeans New PHP Project - selecting name and location
After you click "next" you will be presented with the screen for run configuration. "Run As" should be set to "Local web site" and "Project URL" to "http://localhost/techytalk/". Leave the checkbox unchecked and click next two times.
Now you are presented with index.php inside Netbeans IDE and you can start creating your new web site. You can go to Run -> Run Project to show your web site in your browser or Debug -> Debug project to debug it. When you choose debug you will be given an opportunity to go trough your PHP code line by line and watch state of variables change at the bottom of Netbeans window on "Variables" tab.
This guide is a bit longer so there are many places for both of us to make mistake. So if something doesn't work, comment here (you don't need to register) and I'll do my best to help you out.

No comments:

Post a Comment