In this tutorial we will be setting up a LEMP stack , which includes Nginx, MySQL 5 or MariDB 10, PHP 5 on Debian 7 Wheezy.
This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has
sudo
access and that you precede each of the privileged commands withsudo
Install Nginx
To serve web pages to our visitors, we will be installing Nginx, a modern and efficient web server. For this we will be using Debian’s apt package management suite to complete the installation.
Now we will update apt and install the packages required .
apt-get update
apt-get install -y nginx
Start the Nginx web server:
/etc/init.d/nginx start
If the ufw
firewall is running, we will need to allow the port 80 to take traffic. We can enable this port by following command.
ufw allow 'Nginx HTTP'
Type in your server’s IP address or hostname or http://localhost
and you will see the following default output.
If we get an output anything similar to the above, then we have installed the Nginx successfully.
Install MySQL or MariaDB
There is an option in between installing MySQL or MariaDB, which can be seen as a drop in replacement for MySQL. In MariaDB there all the enterprise features present but without all the corporate structuring.It is managed by the MariaDB Foundation as opposed to being managed by Oracle which manages MySQL. That said, they are both good choices.
Don’t install both, only one needs to be installed.
Install MySQL
Since we are installing a web server, we need to have a database to manage data for our site. For this we are installing MySQL database.
apt-get -y install mysql-server mysql-client
Now we need to configure the Database to complete the installation
mysql_secure_installation
We will be asked to enter the password for the MySQL root account, since
we haven’t set this yet, so just hit ENTER . Then we will be asked to set that password. we should type Y to set a root password.
To rest of all the questions, the script asks, you should press Y, followed by the ENTER key at each prompt. During this process we will remove anonymous users and the test database, disable remote root logins, and load these new rules MySQL.
Install MariaDB
NOTE :This is an optional step, since MySQL is already installed. If you want to go with MariaDB instead of MySQL, then this for you.
Since MariaDB is also an option, as mentioned earlier, and this means it can be installed just like MySql without any further steps. We will be installing MariaDB
Please add the repository as shown below to install MariaDB
apt-get install python-software-properties
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
add-apt-repository 'deb [arch=amd64,i386] http://www.ftp.saix.net/DB/mariadb/repo/10.1/debian wheezy main'
apt-get -y install mariadb-server mariadb-client
Similar to MySQL server, a server needs to be setup as well.
mysql_secure_installation
Install PHP
Now to generate dynamic content, we need to install PHP. Nginx does not contain native PHP processing like other web servers, so we need to install fpm ( “fastCGI process manager” ). fpm will take all the PHP requests from Nginx web server.
apt-get install php5-fpm
Now we have installed PHP and in the coming part, we will make Nginx to use it.
Configure Nginx To Use PHP
Now we need to make Nginx to communicate with PHP for dynamic content.
First open etc/php5/fpm/php.ini file and make cgi.fix_pathinfo to zero, as shown below
vi /etc/php5/fpm/php.ini
- /etc/php5/fpm/php.ini
-
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's ; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok ; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting ; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting ; of zero causes PHP to behave as before. Default is 1. You should fix your scripts ; to use SCRIPT_FILENAME rather than PATH_TRANSLATED. ; http://php.net/cgi.fix-pathinfo cgi.fix_pathinfo=0
Next for this configuration, we will avoid the default directories of Nginx by creating
new configuration file and new root directory to hold our PHP files.We name the new files with our domain or host names as required.
Then create a new configuration file in Nginx’s sites-available
/etc/nginx/sites-available/codesposts.com.conf
- /etc/nginx/sites-available/codesposts.com.conf
-
server { listen 80; listen [::]:80; root /var/www/codesposts.com; index index.php index.html index.htm; server_name codesposts.com; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php5.0-fpm.sock; } }
This simple configuration listens on port 80 and serves files from the web root we just created. Nginx will respond to requests for the name provided after server_name
, and any files ending in .php
will be processed by the php-fpm
process before Nginx sends the results to the user.
Now save and close the file after this basic configuration.
Now we will link our new config file to Nginx’s sites-enabled
directory:
ln -s /etc/nginx/sites-available/codesposts.com.conf /etc/nginx/sites-enabled/
This above step informs Nginx to use the configuration next time it is reloaded. Now we will test our configuration for any syntax errors by the below command.
nginx -t
If errors are reported,we need to check our config file before continuing.
If no errors were reported, then reload Nginx to make the changes:
/etc/init.d/nginx restart
Test PHP Configuration
Now we need test our LEMP stack, to validate that Nginx can correctly hand .php
files by our PHP processor.
To do this we need to create a test PHP file in our document root. We will create a codesposts.php file in the document root.
vi /var/www/html/codesposts.php
Now paste the below code in the blank file we have opened. Which will show the info of our server.
- /var/www/codesposts.com/test.php
-
<?php phpinfo(); ?>
After pasting the above code to our codesposts.php file, save and close the file.
Now we can visit this web page in a web browser from the below shown URL.
http://<Your_serverIP>/codesposts.php
we should see a web page that has been generated by PHP with information about your server. If we get this page, then we can assume that we have successfully configured our LEMP stack.
Now for security reasons, we can delete this test.php file, so that no others can get the info about our servers.
Thanks for being with us, hope this help you gain knowledge and we are always ready to help you. For furthers queries or suggestions, kindly drop a comment below.