Ubuntu Netbeans and LAMP server with Xdebug as non-root user

In this article I'll show you how to setup Linux Apache Mysql PHP (LAMP) web development environment on Ubuntu Linux with your web site files in your home directory. This way you can easily develop for web as non-root user. If you are trying to install Linux Apache Mysql PHP on Windows, I'll point you to one of my previous articles where I have explained just that:

Apache, MySQL, PHP server on Windows

My goals for this session are to install Apache, MySQL and PHP with Xdebug module for PHP development debugging. We 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. I will show you how to instruct Apache to make virtual host in your home directory. At the end of it all I will install Netbeans and create sample project to work with our new development environment. So lets get started...

First thing we 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, we will install Xdebug and one of the PHP image manipulation modules any web developer will need in his work, namely GD library. While we're working with apt-get, we will install Netbeans from Ubuntu repository. Here's the command to do this:

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

I usually use directory "public_html" inside my home to keep all of my 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 we keep our web source code should be accessible and writable by this user. So we will designate "www-data" as the owner of "public_html" directory. What's important for us is that we, as users have same permissions like our web servers "www-data" user for "public_html" directory so we could freely do our work. We will accomplish this by adding our selves to the "www-data" group. Now lets create "public_html" directory, adjust its permissions and add our 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 "marko" in this file you must put your own username:

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
 
	DocumentRoot /home/marko/public_html
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /home/marko/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/marko/public_html/error.log
 
	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn
 
	CustomLog /home/marko/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.

DevGenii

A quality focused Magento specialized web development agency. Get in touch!

25 thoughts on “Ubuntu Netbeans and LAMP server with Xdebug as non-root user

  1. Neville Cox

    Marko
    Where you have typed this
    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
    Do you replace the $USER with user name, as I have typed it in as $USER

    Nev

    Reply
  2. Marko Author

    The $USER variable will be automatically replaced with your user name. I must write instructions that work no mater how user name is so I use built in shell environment variable $USER.

    Reply
  3. Ziku

    When I put my file_name.php file in public_html folder it works….
    but when I’m creating a new project in netbeans, new project in creating in a new folder (same as my project name). when I’m using the run command it won’t work and gives me the following error :

    Not Found
    The requested URL /public_html/Test/index.php was not found on this server.

    What should i do now ?

    Reply
    1. Marko Author

      Hello,
      this is probably about permissions, try following:

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

      I have bash script in my /home/$USER/bin that does this from time to time. God luck!

      Reply
          1. Marko Author

            Just create file ph_script.sh place it inside /home/$USER/bin and put following in it:

            #!/bin/bash
             
            sudo chown -R www-data:www-data /home/$USER/public_html
            sudo chmod -R 777 /home/$USER/public_html

            then you can call it manually from your terminal like this:

            ph_script.sh

            Or you can put it to start at boot, use crontab to start it periodically. Alternatively you can also use ACL to modify the default ownership and permission on files created in specific directory but that’s more advanced, I didn’t have time to explore this in detail.

            Reply
  4. Ujjal

    I have installed LAMP and netbeans on Linux 11.
    Now I want to make public_html as localhost.
    I have followed your steps from this command
    sudo apt-get install php5-xdebug php5-gd

    But its not working. When I hit localhost/info.php
    it says Error 403
    Forbidden

    You don’t have permission to access /ujjal on this server.
    Apache/2.2.17 (Ubuntu) Server at localhost Port 80

    help me please

    Reply
    1. Marko Author

      Hello Ujjal,
      did you replace /home/marko/public_html with /home/YOUR_LOGIN_NAME/public_html in two places inside /etc/apache2/sites-available/public_html? Also you must run those chmod and chown commands from this guide? Did you restart apache using sudo service apache2 restart?

      Reply
        1. Carlos

          Fixed the problem replacing
          Order allow,deny
          allow from all

          with

          Require all granted

          which I copied from the original .conf file.

          Thanks for the great article!

          Reply
  5. alan

    I have followed your instructions until 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”:

    But, I don’t see PHP in New Project. All I can see is:
    Java
    Netbeans Modules
    Samples

    this is all in the left Window.
    How do I find the PHP?
    thanks Alan

    Reply
    1. Marko Author

      Hello,
      Netbeans can be used to develop for many technologies besides PHP so you must ensure you have its PHP plugin installed. Just go to Tools ->Plugins->Available plugins and search for PHP and click install. After your restart Netbeans you’ll have PHP for new project. Also you should consider two things. One, to install latest Netbeans with PHP plugin included from here, download PHP version. Do chmod +x downloaded_file and then ./downloaded_file if you want to install somewhere in your home folder for you only or sudo ./downloaded_file if you want to install globally for all users. Second thing is that better and simpler way to do things described in this article is to use userdir Apache module (I’ll change this article to use this way when I find time). I wrote separate article on enable user dir apache module topic, it’s all explained there.

      Best regards,
      Marko

      Reply
  6. Tex

    Hi,

    many thanks for the guide, it seems all correct but I always receive a 403 Forbidden message…

    I’ve executed the following steps:

    echo $USER
    tex

    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

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

    the content of /etc/apache2/sites-available/public_html is this:

    ServerAdmin webmaster@localhost
    DocumentRoot /home/tex/public_html

    Options FollowSymLinks
    AllowOverride None

    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all

    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/”

    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128

    sudo a2dissite default
    sudo a2ensite public_html

    then reboot my linux box (ubuntu 11.04)

    I’ve created a folder and index.php file under public_html

    mkdir public_html/test
    gedit public_html/test/index.php

    content of public_html/test/index.php

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

    Point the browser on http://localhost/test/index.php and I receive 403 Forbidden: why ?

    Notice that http://localhost/phpmyadmin works well…

    Can you help me please ?

    Reply
    1. Marko Author

      Hello. Looks like WP comment system chewed up some of the code you’ve posted. You should place code inside code tags to avoid this. First thing I would try is to access using 127.0.0.1 instead of localhost because that is what you’ve specified in Allow from 127.0.0.0/255.0.0.0 ::1/128 line. Also you can consider adjusting access settings using gufw (firewall) on your PC and using standard allow from all for apache configuration. Good luck!

      Reply
  7. Mehmet Tayyar

    Hi I have trouble .. I did everthing you said and everything ok.However if I run index.php it locates /home/user/public_html/project browser saves php file ,not run how can I solve it … thanks

    Reply
  8. jose serrano

    Hello maybe i sound ***** but i am new in coding and i love linux mint 14 nadia. after all the steps that you given me i just don’t no where to( or how to find “Document root” to change the username and after that i don’t no how to find the netbean server to start coding php. I hope you bear with me be cos i am a newbie but! all the steps that you gave me were done very correctly.
    I hope i haven’t wasted your time.

    Thank you.

    Reply
  9. Frank Martin

    Very Informative tutorial,
    i am faving an issue.
    using Linux mint 14, all went smooth but wheni try to creat a new projet Neatbeans stuck on loading. i have a working JR6. can u help what is the issue?

    Reply
  10. Bhanu

    Dear Marko,
    Good day to you!!

    i have ubuntu 14.04 installed netbeans then lamp
    i got error below:
    Not Found

    The requested URL /MyFirstPHP/index.php was not found on this server.
    Apache/2.4.7 (Ubuntu) Server at localhost Port 80

    but i have created a file named testphp.php on /var/www/html
    thats running well what must i do to run my netbeans php application.
    Thanks in advanced..
    https://twitter.com/bpsingh2013

    Reply
  11. motiur rahman

    sudo a2dissite default
    sudo a2ensite public_html

    Every thing is ok but I did not run this two command. So my project do not run.Please help

    Thnaks.

    Reply
  12. Guillermo

    First at all, thank you Marko for your article. Just one comment for those people, like me, that are quite new in Debian. I followed the steps and it wouldn’t work… I wondered “What the…”, so I spent like two days browsing the web… finally the permissions of your home folder (in my case “/home/guille”) by default are 700. So no one except you can read anything inside. Just “sudo chmod 755 /home/yourusername”. I hope help someone. Cheers 🙂

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *