How To Setup a LAMP Server on Debian Linux 8 (Jessie)

Posted on in Categories , , last updated August 10, 2015

How can I setup up a LAMP (Linux, Apache, MySql, PHP) stack on Debian Linux version 8 (Jessie) using command line options?

Setting up a LAMP stack on your Debian 8 server will allow for the hosting of websites and web applications written in PHP, Perl, and Python. You need to install the following packages on Debian Linux 8:

  1. apache2 : Apache HTTP Server
  2. mysql-server: MySQL Server
  3. php5 : PHP 5
  4. php-pear: PHP 5 pear package
  5. php5-mysql: PHP 5 mysql support

In this tutorial, you will learn installing LAMP on your Debian 8 server.

Update your system

Type the following command:
# apt-get update
# apt-get upgrade

Install Apache 2 package

Type the following apt-get command to instal Apache httpd server version 2:
# apt-get install apache2
Sample outputs:

Fig. 01: Install Apache 2 on Debian 8
Fig. 01: Install Apache 2 on Debian 8

Install MySQL server package

Type the following apt-get command to instal Mysql server version 5.x:
# apt-get install mysql-server

Install PHP package

Type the following apt-get command to instal php5:
# apt-get install php5 php-pear libapache2-mod-php5

Install PHP modules

Type the following apt-get command to instal php modules:
# apt-get install php5-mysql php5-gd
To find out additional PHP5 modules, enter:
# apt-cache search php5-
Sample outputs:

php5-exactimage - fast image manipulation library (PHP bindings)
php5-gdcm - Grassroots DICOM PHP5 bindings
php5-vtkgdcm - Grassroots DICOM VTK PHP bindings
php5-geos - GEOS bindings for PHP
php5-lasso - Library for Liberty Alliance and SAML protocols - PHP 5 bindings
php5-libvirt-php - libvirt bindings for PHP
php5-mapscript - php5-cgi module for MapServer
php5-adodb - Extension optimising the ADOdb database abstraction library
php5-apcu - APC User Cache for PHP 5
php5-gearman - PHP wrapper to libgearman
php5-geoip - GeoIP module for php5
php5-gnupg - wrapper around the gpgme library
php5-igbinary - igbinary extension
php5-imagick - Provides a wrapper to the ImageMagick library
php5-json - JSON module for php5
php5-memcache - memcache extension module for PHP5
php5-memcached - memcached extension module for PHP5, uses libmemcached
php5-mongo - MongoDB database driver
php5-msgpack - PHP extension for interfacing with MessagePack
php5-mysqlnd-ms - MySQL replication and load balancing module for PHP
php5-oauth - OAuth 1.0 consumer and provider extension
php5-pecl-http - pecl_http module for PHP 5 Extended HTTP Support
php5-pecl-http-dev - pecl_http module for PHP 5 Extended HTTP Support development headers
php5-pinba - Pinba module for PHP 5
php5-propro - propro module for PHP 5
php5-propro-dev - propro module for PHP 5 development headers
php5-radius - PECL radius module for PHP 5
php5-raphf - raphf module for PHP 5
php5-raphf-dev - raphf module for PHP 5 development headers
php5-redis - PHP extension for interfacing with Redis
php5-rrd - PHP bindings to rrd tool system
php5-sasl - Cyrus SASL Extension
php5-solr - solr module for PHP 5
libssh2-php - transitional dummy package for php5-ssh2
php5-ssh2 - Bindings for the libssh2 library
php5-stomp - Streaming Text Oriented Messaging Protocol (STOMP) client module for PHP 5
php5-svn - PHP Bindings for the Subversion Revision control system
php5-tokyo-tyrant - PHP interface to Tokyo Cabinet's network interface, Tokyo Tyrant
php5-yac - YAC (Yet Another Cache) for PHP 5
php5-zmq - ZeroMQ messaging
libphp5-embed - HTML-embedded scripting language (Embedded SAPI library)
php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
php5-dev - Files for PHP5 module development
php5-enchant - Enchant module for php5
php5-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary)
php5-gd - GD module for php5
php5-gmp - GMP module for php5
php5-imap - IMAP module for php5
php5-interbase - interbase/firebird module for php5
php5-intl - internationalisation module for php5
php5-ldap - LDAP module for php5
php5-mcrypt - MCrypt module for php5
php5-mysql - MySQL module for php5
php5-mysqlnd - MySQL module for php5 (Native Driver)
php5-odbc - ODBC module for php5
php5-pgsql - PostgreSQL module for php5
php5-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary)
php5-pspell - pspell module for php5
php5-readline - Readline module for php5
php5-recode - recode module for php5
php5-snmp - SNMP module for php5
php5-sqlite - SQLite module for php5
php5-sybase - Sybase / MS SQL Server module for php5
php5-tidy - tidy module for php5
php5-xmlrpc - XML-RPC module for php5
php5-xsl - XSL module for php5
php5-librdf - PHP5 language bindings for the Redland RDF library
php5-remctl - PECL module for Kerberos-authenticated command execution
php5-twig - Enhance performance of the Twig template engine
php5-uprofiler - hierarchical profiler for PHP (extension)
php5-xcache - Fast, stable PHP opcode cacher
php5-xdebug - Xdebug Module for PHP 5
php5-xhprof - Hierarchical Profiler for PHP5

Optional: Install Perl, for Perl apps

Type the following apt-get command to instal Perl module for Apache 2:
# apt-get install perl libapache2-mod-perl2

Optional: Install Python, for python apps

Type the following apt-get command to instal Python module for Apache 2:
# apt-get install python libapache2-mod-python

LAMP configurations

At this stage required software packages are installed. To find your severs’s IP address, run:
# ifconfig eth0
OR
# ip addr show eth0
Sample outputs:

2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:08:9b:c4:30:30 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::208:9bff:fec4:3030/64 scope link 
       valid_lft forever preferred_lft forever

Fire a web-browser and test it by typing your server IP address:

http://192.168.1.10/

Sample outputs:

Fig.02: The default Apache 2 page on Debian Linux 8
Fig.02: The default Apache 2 page on Debian Linux 8

Configure Apache

Edit /etc/apache2/apache2.conf file, enter:
# vi /etc/apache2/apache2.conf
Make sure you setup port, IP address, and other information as per your needs. Finally, restart the server:
# systemctl restart apache2

How do I configure name based virtual host for domain called cyberciti.biz?

First, create directories, as follows:
# D="cyberciti.biz"
# mkdir -p /var/www/html/$D/html_root
# mkdir -p /var/log/apache2/$D/

Create /etc/apache2/sites-available/$D.conf file:
# vi /etc/apache2/sites-available/$D.conf
Append the following directives:

<VirtualHost *:80> 
     ServerAdmin [email protected]
     ServerName cyberciti.biz
     ServerAlias www.cyberciti.biz
     DocumentRoot /var/www/html/cyberciti.biz/html_root
     ErrorLog  /var/log/apache2/cyberciti.biz/logs/error.log 
     CustomLog  /var/log/apache2/cyberciti.biz/logs/access.log combined
</VirtualHost>

Save and close the file. Turn on configuration:
# a2ensite $D.conf
Again, restart apache server:
# systemctl restart apache2

Configure MySQL server

First, secure your mysql server, type:
# mysql_secure_installation
Create a sample mysql database called wordpress. First, login as follows:
# mysql -u root -p
Type the following sql commands at mysql> prompt:

mysql> create database wordpress;
mysql> grant all on wordpress.* to 'vivek' identified by '1SfFAi9$';
mysql> quit

The above will create a database called ‘wordpress’ and grant your users permissions on it with username ‘vivek’ and the password ‘1SfFAi9$’. See how to create and use database/users on MySQL for more info.

Configure PHP

Edit /etc/php5/apache2/php.ini, enter:
# vi /etc/php5/apache2/php.ini
Append or modify as follows:

;
; *******************************************************************************************************
; NOTE: These settings are good starting points, but should be adjusted to best suite your requirements.*
; *******************************************************************************************************

; Set error reporting to a log file
display_errors=Off
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
error_log = /var/log/apache2/php-error.log  

; Again set this as per your needs - Anit DoS settings
max_execution_time =  30
max_input_time = 30
memory_limit = 40M

; No exposing is allowed 
expose_php=Off

; Disable file uploads 
file_uploads=Off

; Turn off remote execution 
allow_url_fopen=Off
allow_url_include=Off

; Block dangerous php functions 
disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

Note: See our php 5 security tips guide and block php functions which are not suppose to use for more information.

Again, restart apache 2 server:
# systemctl restart apache2
Next, create /var/www/html/info.php file to test PHP:
# echo '<?php phpinfo(); ?>' > /var/www/html/info.php
Test url:

http://192.168.1.10/info.php

Sample outputs:

Fig.03 Apache 2 php test page on Debian Linux 8
Fig.03 Apache 2 php test page on Debian Linux 8

Security: Firewall settings

You need to open port 80:
/sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
See our firewall settings guide on Debain for more information.

Security: Permission for /var/www/html/ DocumentRoot directory

Make sure you set files and directories permission as follows:

## Must run as root user ##
# Make sure Apache user owns /var/www/html/
chown -R www-data:www-data /var/www/html
 
# Make sure it is read-only 
chmod -R 0444 /var/www/html/
 
# Make sure Apache can read files in sub-dirs to avoid HTTP/403 status errors 
find /var/www/html/ -type d -print0 | xargs -0 -I {} chmod 0445 "{}"
 
# Do you need to give write permission to certain directories like $D/blog/wp-cache/?
# Edit as per needs :)
# chmod -R 0775 /var/www/html/cyberciti.biz/blog/wp-cache/
# echo 'Deny from all' >> '/var/www/html/cyberciti.biz/blog/wp-cache/'
# EOF

Important commands

To start/stop/restart and to see status of Apache 2, enter:
# systemctl start apache2
# systemctl stop apache2
# systemctl restart apache2
# systemctl status apache2

To start/stop/restart and to see status of MySQL server, enter:
# systemctl start mysql
# systemctl stop mysql
# systemctl restart mysql
# systemctl status mysql

Verify that port # 80 open:
# netstat -tulpn | grep :80
# ss -t -a
# ss -t -a | grep http
# ss -o state established '( dport = :http or sport = :http )'
# iptable -L -n -v | less

Important log files

To see Apache 2 log files, enter:

tail -f /var/log/apache2/access.log
tail -f /var/log/apache2/error.log
grep something /var/log/apache2/error.log
### PHP ##
tail -f /var/log/apache2/php-error.log
### Vhost cyberciti.biz ##
tail -f /var/log/apache2/cyberciti.biz/logs/error.log 
tail -f /var/log/apache2/cyberciti.biz/logs/access.log
grep something /var/log/apache2/cyberciti.biz/logs/error.log

Now, you have installed LAMP stack on your Debian v8.x server.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

4 comment

  1. Regarding

    “ErrorLog /var/log/apache2/cyberciti.biz/logs/error.log
    CustomLog /var/log/apache2/cyberciti.biz/logs/access.log combined”

    I found that I needed to manually create the directory ‘logs’, otherwise there is an error about … .

    Then I was able to restart apache.

Comments are closed.