Install LEMP Server (nginx, MySQL or MariaDB, PHP) On Debian 7 Wheezy

by
Share this Article: Facebook0Google+6Twitter10LinkedIn3Reddit0Pinterest0StumbleUpon1Digg

LEMP is a combination of the operating system and open-source software stack. The acronym LEMP is derived from first letters of Linux, Nginx HTTP Server, MySQL or MariaDB and PHP. We already have shown you how to install LEMP and LAMP servers on many platforms. Search through our archives for the details.

Here my testbox hostname is server.unixmen.com and IP address is 192.168.1.200/24. Change these values with your own where it appropriate.

Note: All commands in this tutorial are executed by root user, so we don’t have to use ‘sudo’ in-front of every command.

Install Nginx

Nginx (pronounced engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server written by Igor Sysoev.

To install Nginx enter the following command in your terminal:

Note: If you have installed apache2 in your system, remove it first to avoid conflicts. To uninstall apache, run the following commands:

apt-get purge apache2*
apt-get autoremove

Now install nginx using command:

apt-get install nginx

Start Nginx service using the command:

service nginx start

Test nginx

Open up your web browser and navigate to http://ip-address/ or http://localhost/. You will see a screen something like below.

Mozilla Firefox_001Configure Nginx

Open the file /etc/nginx/nginx.conf in any editor:

nano /etc/nginx/nginx.conf 

Set the worker_processes (i.e No. of CPU’s in your system). To see the no. of CPU’s, use the command “lscpu”. In my case it’s “1″. So I set this as ’1′.

worker_processes 1;

Restart Nginx service:

service nginx restart

The default vhost is defined in the file /etc/nginx/sites-available/default.

Open the file /etc/nginx/sites-available/default in any editor. Under the Server section, match your configuration as shown below. The modifications are shown in red color.

[...]
server {
        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

        root /usr/share/nginx/www;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name server.unixmen.com;
[...]

Here,

listen 80; –> listen for ipv4

listen [::]:80 default_server ipv6only=on; –> listen for ipv6

root /usr/share/nginx/www; –> document root directory.

server_name server.unixmen.com; –> virtualhost name.

Now scroll down further and find the section #location ~ \.php$. Uncomment and modify as shown below.

 location ~ \.php$ {
         try_files $uri =404;
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
         #
         #       # With php5-cgi alone:
         #       fastcgi_pass 127.0.0.1:9000;
         #       # With php5-fpm:
         fastcgi_pass unix:/var/run/php5-fpm.sock;
         fastcgi_index index.php;
         include fastcgi_params;
    }

Here i added an extra line ‘try_files $uri =404;’ to prevent zero day exploits.

Save and exit the file.

Test nginx configuration

Test the nginx configuration for any syntax errors using command:

nginx -t

Sample output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally restart nginx service

service nginx restart

Install MySQL

MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases, though SQLite probably has more total embedded deployments

apt-get install mysql-server mysql-client

During installation, you’ll be asked to setup the MySQL root user password. Enter the password and click Ok.

sk@sk: ~_002Re-enter the password.

sk@sk: ~_003Now MySQL server has been installed.

You can verify the MySQL server status using command:

service mysql status

Sample output:

[info] /usr/bin/mysqladmin  Ver 8.42 Distrib 5.5.31, for debian-linux-gnu on i686
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Server version        5.5.31-0+wheezy1
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /var/run/mysqld/mysqld.sock
Uptime:            39 sec

Threads: 1  Questions: 565  Slow queries: 0  Opens: 809  Flush tables: 2  Open tables: 194  Queries per second avg: 14.487.

Note: If you want to use MariaDB instead of MySQL, then follow these steps to install MariaDB on Ubuntu 13.10 server.

Install MariaDB

MariaDB is a drop in replacement for MySQL. It is a robust, scalable and reliable SQL server that comes rich set of enhancements.

First you have to remove existing MySQL packages if any. To completely uninstall MySQL with configuration files, enter the following command:

apt-get purge mysql*

Run the following command to remove unwanted packages.

apt-get autoremove

Now add MariaDB PPA to install it. MariaDB PPA is not updated yet to Ubuntu 13.10, but it just worked fine using old PPA i.e Ubuntu 13.04.

Run the following commands to add PPA.

apt-get install python-software-properties
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db 
add-apt-repository 'deb http://mariadb.biz.net.id//repo/5.5/debian wheezy main'

Update the software sources list and install MariaDB using following commands:

apt-get update 
apt-get install mariadb-server mariadb-client

During installation you will be asked to set MariaDB ‘root’ user password. Enter the password and click Ok.

sk@sk: ~_004Re-enter password:

sk@sk: ~_005Check if mariadb service is running or not, using the following command:

service mysql status

Sample output:

[info] /usr/bin/mysqladmin  Ver 9.0 Distrib 5.5.33a-MariaDB, for debian-linux-gnu on i686
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Server version        5.5.33a-MariaDB-1~wheezy-log
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /var/run/mysqld/mysqld.sock
Uptime:            48 sec

Threads: 1  Questions: 1172  Slow queries: 0  Opens: 782  Flush tables: 4  Open tables: 175  Queries per second avg: 24.416.

Install PHP

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely used open-source general purpose scripting language that is especially suited for web development and can be embedded into HTML.

Install PHP with following command:

apt-get install php5 php5-fpm php5-mysql

Configure PHP

We have to do a small change in php.ini file. Open php.ini file in any editor:

nano /etc/php5/fpm/php.ini

Find the line ‘cgi.fix_pathinfo=1′, uncomment it and change the value 1 to 0.

cgi.fix_pathinfo=0

Now restart php-fpm service.

service php5-fpm restart

Test PHP

Create a sample “testphp.php” file in nginx document root folder.

nano /usr/share/nginx/www/testphp.php

Add the following lines in it.

<?php
 phpinfo();
 ?>

Save and exit the file.

Navigate to  http://server-ip-address/testphp.php. It will display all the details about php such as version, build date and commands etc.

phpinfo() - Mozilla Firefox_006PHP-FPM listens on the socket /var/run/php5-fpm.sock by default. If you want to make PHP-FPM use a TCP connection, open the file /etc/php5/fpm/pool.d/www.conf,

nano /etc/php5/fpm/pool.d/www.conf

Find the line listen = /var/run/php5-fpm.sock, modify it as listen = 127.0.0.1:9000.

;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000

Save and exit the file. Restart php5-fpm service.

service php5-fpm restart

Now open the nginx configuration file:

nano /etc/nginx/sites-available/default

Find the line fastcgi_pass unix:/var/run/php5-fpm.sock; and change it to fastcgi_pass 127.0.0.1:9000;, as shown below.

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
   }

Save and exit the file. Finally restart nginx service.

service nginx restart

Manage MySQL Databases Graphically (Optional)

phpMyAdmin:

phpMyAdmin is a free open-source web interface tool used to manage your MySQL databases.

Install phpMyAdmin

It is available in the Official Debian repositories. So install it with command:

apt-get install phpmyadmin

Select any webserver. Here nginx will not be displayed. Let us link phpmyadmin to work with nginx webserver later.

sk@sk: ~_007Select Ok to configure database for phpmyadmin wjth dbconfig-common.

sk@sk: ~_008Select Yes to continue.

sk@sk: ~_009Enter password of the database’s administrative user.

sk@sk: ~_010Enter MySQL application password phpmyadmin.

sk@sk: ~_011Re-enter the password.

sk@sk: ~_012The phpMyAdmin installation has been completed.

Create a symbolic link between phpMyAdmin and the website root directory. Here our website root document directory is /usr/share/nginx/html/.

ln -s /usr/share/phpmyadmin/ /usr/share/nginx/www/

Restart nginx server.

service nginx restart

Access phpMyAdmin Web Console

Now you can access the phpmyadmin console by navigating to http://server-ip-address/phpmyadmin/ from your browser.

Enter your MySQL username and password which you have given in previous steps. In my case its “root” and “debian”.

phpMyAdmin - Mozilla Firefox_013You will be redirected to phpMyAdmin main web interface.

192.168.1.200 - localhost | phpMyAdmin 3.4.11.1deb2 - Mozilla Firefox_014Now you can manage your MySQL databases from phpMyAdmin web interface.

That’s it. Your LEMP server is up and running now.

For questions please refer to our Q/A forum at : http://ask.unixmen.com/

Share this Article: Facebook0Google+6Twitter10LinkedIn3Reddit0Pinterest0StumbleUpon1Digg