Similar to the LAMP server (Linux, Apache, MySQL, PHP) stack, LEMP Server (Linux, Nginx or engine-x, MySQL, PHP-FPM or PHP-FastCGI) is an alternative stack of open source software for web server. Unlike Apache, Nginx can increase web server scalability by optimize the web server hardware resources (cpu, memory…) which lead to a very cost effective architecture.
Normally Apache creates a new process/thread for each request which may cause I/O bottleneck. Every new process has to wait for old process to finish and release the server resources (cpu, memory) in hole. It means if you have a busy website, your web server will have many seperate httpd processes hanging and using more resources.
To solve Apache thread/process problem, Nginx is event driven and uses Reactor pattern, Nginx does not create a new process for a new request. When nginx receives request, trigger events in a process. The process handles all the events and returns the output.
Install nginx repository
nginx package does not come as default package for Centos or RHEL (Red Hat Enterprise Linux). To install nginx, you have to add nginx yum repository or directly install rpm package.
Create a file named nginx.repo in /etc/yum.repos.d/ directory
# nano /etc/yum.repos.d/nginx.repo
with the content
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/6/$basearch/ gpgcheck=0 enabled=1
For Centos 5.x
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/5/$basearch/ gpgcheck=0 enabled=1
For RHEL 6.x (Red Hat Enterprise Linux)
[nginx] name=nginx repo baseurl=http://nginx.org/packages/rhel/6/$basearch/ gpgcheck=0 enabled=1
For RHEL 5.x (Red Hat Enterprise Linux)
[nginx] name=nginx repo baseurl=http://nginx.org/packages/rhel/5/$basearch/ gpgcheck=0 enabled=1
Install nginx web-server
It’s pretty straightforward to install nginx web server on Centos or RHEL
# yum install nginx
To start nginx
# /etc/init.d/nginx start
or
# service nginx start
To run Apache automatically when the system boot/reboot
# chkconfig nginx on
By now you should have your nginx web server running.
In case you can’t access your nginx web server, it’s might be a iptables firewall problem. Disable your iptables temporary
# iptables -F # iptables -P OUTPUT ACCEPT # iptables -P INPUT ACCEPT # iptables -P FORWARD ACCEPT # iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT # iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT # iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # iptables-save | sudo tee /etc/sysconfig/iptables # service iptables restart
Install MySQL database serer
There are many great database server out there, but in this article we are going to use MySQL which is very popular.
# yum install mysql mysql-server
To start MySQL
# /etc/init.d/mysqld start
or
# service mysqld start
To run MySQL automatically when the system boot/reboot
# chkconfig mysqld on
Secure MySQL
# /usr/bin/mysql_secure_installation
By running mysql_secure_installation script, you should set mysql root’s password, Remove anonymous users, Disallow root login remotely. Simply type in your mysql root’s password, and y for yes everything else.
Set root password? [Y/n] y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] y ... Success! By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL!
Install PHP or PHP-FPM
# yum install php-fpm php-common php-mysql php-pear php-gd php-devel php-mbstring php-cli php-pdo php-xml
To start PHP-FPM
# /etc/init.d/php-fpm start
or
# service php-fpm start
To run PHP-FPM automatically when the system boot/reboot
# chkconfig php-fpm on
Configure nginx to work with PHP
# nano /etc/nginx/conf.d/default.conf
Modify / append as follows:
location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
Search for
location / { root /usr/share/nginx/html; index index.html index.htm; }
add index.php to index line
location / { root /usr/share/nginx/html; index index.html index.htm index.php; }
Save and close the file. Restart nginx:
# service nginx restart
To confirm nginx and PHP are working
Default nginx html/php files will be stored in /usr/share/nginx/html . You can change nginx default document root directory in /etc/nginx/conf.d/default.conf if you wish to. By the way we are creating php test file to check if nginx and php are working
# nano /usr/share/nginx/html/info.php
With the content
<?php phpinfo(); ?>
Save and exit, fire up your website and go to info.php file by going http://yourdomain/info.php