Showing posts with label Nginx. Show all posts
Showing posts with label Nginx. Show all posts

Saturday, December 5, 2015

Setting up a website Ngnix Mysql PHP(LEMP) on CentOS x86&x64

https://discuss.vultr.com/discussion/256/setting-up-a-website-ngnix-mysql-phplemp-on-centos-x86x64/p1

LEMP means Nginx MySQL and PHP installed on Linux. Sometime it also called LNMP. Nginx is a good alternative to Apache webserver for building websites on low memory hosts.

This How-To is telling you to install LEMP by yum, NOT by compiling from their Tarball. It is saving your time and effort.

Here is the steps to how to install, I used the Centos6.5_x86 as the Linux system, All steps are running as root:

1. Preparation
The default CentOS repositories do not have Nginx so we need to add the Atomic Corp. repository. Adding this repository is easy.

[root@vultr ~]# wget -qO - http://www.atomicorp.com/installers/atomic | sh

It will show their agreement on-screen , accept it will install the repo for you.

Then run below command to check-update

[root@vultr ~]# yum check-update

2. Nginx installation
Nginx on Atomic Corp. repository is available on version 1.6.0, To install is to run:
[root@vultr ~]# yum install nginx
Answer “y” when the screen appeared some messages. Start the web service Nginx is to run:
[root@vultr ~]# service nginx start
Now open the address http://serverip ,You will see the nginx default welcome page. It tells you the nginx works well. If you web browser can’t show the page. Most of time it is because of the Centos system firewall. So we should run below commands to change the firewall setting.
[root@vultr ~]# vi /etc/sysconfig/iptables
Modify the iptables to add two lines in it. The added two rules must right after the line “-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT” . DO NOT insert them in the end of file. This is very critical. Here is the modified sample. It only shows modified part of it:
#########################################################

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited

#########################################################

After that we must restart iptables by run this,

[root@vultr ~]# service iptables restart
Now try again via your web browser to access http://serverip :

Tips: You can find your VPS public IP from vultr website control panel or use the ifconfig command in SSH.

3. MySQL installation
Mysql on Atomic Corp. repository is available on version 5.5, To install is to run:
[root@vultr ~]# yum install mysql mysql-server
Answer “y” when the screen appeared some messages. Then do a secure installation to set the root password, Then Let's run the mysql:
[root@vultr ~]# service mysqld start
[root@vultr ~]# mysql_secure_installation
Before you run "mysql_secure_installation", you must make sure the mysql is running on your CentOS system. If it prompts “Enter current password for root (enter for none);” Please press Enter key to continue. After set the root password for mysql, You could restart the mysql is to run:
[root@vultr ~]# service mysqld restart

4. PHP installation
Run command below to install PHP and Some PHP components:
[root@vultr ~]# yum install php php-fpm
[root@vultr ~]# yum install php-mysql php-gd libjpeg-turbo-devel libjpeg-turbo-static php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel

5. Nginx Configuration
Firstly, We could set the nginx mysql and php will run automatically after booting the VPS each time. To set is to run:
[root@vultr ~]# chkconfig nginx on
[root@vultr ~]# chkconfig mysqld on
[root@vultr ~]# chkconfig php-fpm on

The nginx configuration file is available here: /etc/nginx/nginx.conf

One thing about this file is you could modify access log setting. In my experience, if you don’t keep it default, the error.log will be very big, It may reach 10G. Here is the modified sample of it, It only shows modified part of the whole nginx.conf :
#########################################################

#error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;

#########################################################

To modify the default site example:
[root@vultr ~]# vi /etc/nginx/conf.d/default.conf
Two places you should change, one is the index setting(Line 14), another one is set the php works together with nginx(Line 40-46). Be careful on Line 44 content!! Here is the modified sample of it, It only shows modified part of it:
##########################################################
#
#
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
#
#
#
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#
#
#
##########################################################

After change it, Remember to restart the nginx:
[root@vultr ~]# service nginx restart

6. PHP-fpm configuration
The php-fpm configuration file is available here: /etc/php-fpm.d/www.conf
The default setting is running user is apache, We could change it to user:nginx and group:nginx (Line 39 and 41).
[root@vultr ~]# vi /etc/php-fpm.d/www.conf
##########################################################


; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

##########################################################

After change it, Remember to restart the php-fpm:
[root@vultr ~]# service php-fpm restart
Now let’s test if php is working well, To create php test page is to run:
[root@vultr ~]# vi /usr/share/nginx/html/info.php
Add following 3 lines and save the file:
##########################################################
<?php<br />
phpinfo();
?>
##########################################################
When you copy it, please maunally delete the unnecessary part after "php" in the first line.

Go to http://serverip/info.php in your web browser and you’ll see the php infomation:

7. phpMyAdmin installation:
This step is optional, you can choose to run it or not. on Atomic Corp. repository phpMyAdmin is available, to install is to run:
[root@vultr ~]# yum install phpmyadmin
The default installation is /usr/share/phpMyAdmin, but our default website root directory is /usr/share/nginx/html, To fix it is to create a symbolic link between phpMyAdmin and your site’s directory. Be carefully, the letter is case sensitive.
[root@vultr html]# ln -s /usr/share/phpMyAdmin/ /usr/share/nginx/html/phpmyadmin
Finally you can access phpMyAdmin via http://serverip/phpmyadmin . If there is a error, try to refresh it via your web browser. Finally It will appear a window, input username (default is root) and password (We set it on step 3)

Done!

All steps are tested successfully on CentOS6_i386 and CentOS6_X64 of vultr VPS. Good luck to you!

BTW: This How-To is also applicable in CentOS5_i386 and CentOS5_x64. Only difference is the changing iptables setting on the step 2. Here is the modified sample /etc/sysconfig/iptables on CentOS5_i386 and CentOS5_x64. It only shows modified part of it:
#########################################################
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

#########################################################

Using Nginx with Virtualmin

http://www.virtualmin.com/documentation/web/nginx

Introduction to Nginx

Nginx is a lightweight webserver that supports most of the functionality of Apache, but is faster and uses less memory. It is suited to websites that have a large amount of static content, or virtual machines with limited memory. For more information, see http://wiki.nginx.org/Main

Installing Nginx Support

Switching a system from the Apache webserver (installed by default by Virtualmin) to Nginx should only be done if no virtual servers with websites have been created yet. Ideally the change should be done on a freshly installed system, running RHEL 6.0, CentOS 6.0 or Debian 6.0 or later.
The steps to remove Apache and install Nginx are :
  1. Shut down Apache with the command service httpd stop ; chkconfig httpd off (on RHEL or CentOS), or /etc/init.d/apache2 stop ; update-rc.d apache2 remove (on Debian).
  2. Install Nginx with the command yum install nginx (on RHEL or CentOS) or apt-get install nginx (on Debian).
  3. Start the Nginx webserver with the command /etc/init.d/nginx start
  4. Install Virtualmin's Nginx plugin with the command yum install wbm-virtualmin-nginx wbm-virtualmin-nginx-ssl (on RHEL or CentOS) or apt-get install webmin-virtualmin-nginx webmin-virtualmin-nginx-ssl (on Debian).
Once this is done, you can configure Virtualmin to use it as follows :
  1. Login to Virtualmin as root, and go to Webmin -> Servers -> Nginx Webserver and make sure that Nginx and its configuration files are found.
  2. If not, click on the Module Config link and set the config and command paths correctly.
  3. Return to Virtualmin, and go to System Settings -> Features and Plugins.
  4. Un-check the "Apache website" , "SSL website" and "DAV Login", "Mailman", "Protected web directories", "AWstats reporting" and "Subversion repositories" features.
  5. Check the "Nginx website" and "Nginx SSL website" features, then click "Save".
  6. If Virtualmin reports that any other features cannot be used without Apache, go back and de-select them too.
  7. Go to the System Information page and click Refresh system information in the top right.
  8. Verify that Nginx is shown as running in the "Status" section.

Using Nginx

Once Nginx support has been configured, you should be able to create virtual servers just as you would with Apache. However, on the Create Virtual Server page you will need to select Enable Nginx website? in the Enabled features section, instead of Enable Apache.
When creating a domain from the command-line API, you will need to use the --virtualmin-nginx flag instead of --web . For SSL websites, you will need to use --virtualmin-nginx-ssl instead of --ssl .
Similarly, when creating a domain via the remote API, you will need to use the virtualmin-nginx= parameter instead of web= .

Limitations of Nginx

Nginx as configured by Virtualmin lacks some features of Apache, such as :
  • Only one virtual server can have SSL enabled per IP address, even if a wildcard or UCC certificate would potentially allow multiple SSL sites to share the same IP.
  • Nginx does not support CGI, so any applications or Virtualmin scripts that use CGI will not work. Virtualmin should prevent the installation of scripts that require CGI, mod_perl or Apache-specific features.
  • PHP can only be executed via FastCGI, and all PHP scripts run with domain owner permissions. Execution via CGI or mod_php is not supported. For PHP to work, Virtualmin will setup a PHP FastCGI server process that Nginx communicates with for each virtual server.
  • Only one PHP version is supported, and Virtualmin will pick the highest version available on the system. This typically means that PHP v4 scripts cannot be run.
Spanish language by Maria Ramos from Webhostinghub.com/support/edu.