This howto guide explains more deeply on how to install LEMP (Linux, Nginx, MySQL, PHP) on pre-installed Linux servers and installation instructions can be used on CentOS 6.7 servers using Remi and Nginx repositories with YUM command.
In this guide we use latest MySQL 5.5.49, PHP 5.6.21 and Nginx/1.10.0 versions with PHP-FPM modules with Nginx. So, before moving further for installation let’s discuss about Nginx and PHP-FPM.
What is Nginx?
Nginx (Engine X) is open source robust light and high performance Web server, reverse proxy sever and also mail proxy server for HTTP, SMTP, POP3 and IMAP protocols. To know more about features visit http://wiki.nginx.org/Overview
What is PHP-FPM?
Step 1: Installing Remi Repository
1 2 |
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm |
Step 2: Installing Nginx Repository
Create file /etc/yum.repos.d/nginx.repo and add following content to repo file:
1 2 3 4 5 |
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 |
Step 3: Installing Ngnix 1.10.0, MySQL 5.5.49, PHP 5.6.21 & PHP-FPM
Install NGINX with the following command:
1 |
yum install nginx |
Start the NGINX service with the following command:
1 |
/etc/init.d/nginx start |
Configure NGINX to start when the system is rebooted:
1 2 |
chkconfig --add nginx chkconfig nginx on |
You will now have NGINX installed on your server. This can be verified by typing in the following with your IP ADDRESS on your browser. Also, all configuration files are provided on the page.
We can now verify NGINX is working by opening your browser and entering the URL http://your-server's-address
. you should get a blue Nginx test page.
How To Set Up nginx Virtual Hosts
Install MySQL with the following command to begin the install:
1 |
yum --enablerepo=remi install mysql-server |
Start the service with the following command:
1 |
/etc/init.d/mysqld start |
You can do some configuration of MySQL with this command:
1 |
/usr/bin/mysql_secure_installation |
The prompt will ask you for your current root password.
Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter.
1 2 |
Enter current password for root (enter for none): OK, successfully used password, moving on... |
Then the prompt will ask you if you want to set a root password. Go ahead and choose Y and follow the instructions.
CentOS automates the process of setting up MySQL, asking you a series of yes or no questions.
It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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! |
Configure MySQL to start when the system is rebooted:
1 2 |
chkconfig --add mysqld chkconfig mysqld on |
Installing and Configuring php-fpm on CentOS 6.7 for LEMP
Install php-fpm with the following command:
1 |
yum --enablerepo=remi-php56 install php php-fpm php-devel php-mysql |
PHP Modules
PHP also has a variety of useful libraries and modules that you can add onto your server. You can see the libraries that are available by typing:
1 |
yum --enablerepo=remi-php56 search php- |
Terminal then will display the list of possible modules. The beginning looks like this:
1 2 3 4 5 6 7 8 9 |
php-bcmath.x86_64 : A module for PHP applications for using the bcmath library php-cli.x86_64 : Command-line interface for PHP php-common.x86_64 : Common files for PHP php-dba.x86_64 : A database abstraction layer module for PHP applications php-devel.x86_64 : Files needed for building PHP extensions php-embedded.x86_64 : PHP library for embedding in applications php-enchant.x86_64 : Human Language and Character Encoding Support php-gd.x86_64 : A module for PHP applications for using the gd graphics library php-imap.x86_64 : A module for PHP applications that use IMAP |
To see more details about what each module does, type the following command into terminal, replacing the name of the module with whatever library you want to learn about.
1 |
yum --enablerepo=remi-php56 info name_of_the_module |
Once you decide to install the module, type:
1 |
yum --enablerepo=remi-php56 install php-mbstring php-mcrypt php-soap php-apc php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel |
Start the php-fpm service with the following command:
1 |
/etc/init.d/php-fpm start |
Make sure php-fpm starts on boot with the following command:
1 2 |
chkconfig --add php-fpm chkconfig php-fpm on |
Configure php
Using your favorite editor, edit the file /etc/php.ini
and change the default timezone. It should look similar to the block below.
1 2 3 4 |
[Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = UTC |
And find the line, cgi.fix_pathinfo=1
, and change the 1 to 0.
1 |
cgi.fix_pathinfo=0 |
If this number is kept as a 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative. Save and Exit.
Using your favorite editor, edit the file /etc/php-fpm.d/www.conf
and change user and group from apache to nginx. It should look similar to the block below.
1 2 3 4 5 6 7 |
; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; 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 |
Finish by restarting php-fpm.
1 |
/etc/init.d/php-fpm restart |
Now we need to make some changes to the Nginx configuration file so that php-fpm works correctly with Nginx. Using your favorite editor, edit the file /etc/nginx/conf.d/default.conf
and carry out the following changes or copy the following block below into your conf file.
1) Add the index.php to the index location
2) Change the root location to /usr/share/nginx/html
3) Uncomment the Pass PHP scripts to FastCGI section.
4) Change the fastcgi_param SCRIPT_FILENAME
to use /usr/share/nginx/html$fastcgi_script_name
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
server { listen 80; server_name localhost; root /usr/share/nginx/html; client_max_body_size 20M; location / { index index.php index.html; } # serve static files directly location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ { access_log off; } location ~ \.php$ { try_files $uri =404; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_buffer_size 128k; fastcgi_buffers 256 4k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; } location ~ /\. { deny all; access_log off; log_not_found off; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } } |
Create a test PHP file in the following directory with the following command:
1 |
nano /usr/share/nginx/html/info.php |
Add in the following line:
1 2 3 |
<?php phpinfo(); ?> |
Then Save and Exit.
Restart nginx so that all of the changes take effect:
1 |
/etc/init.d/nginx restart |
Test your page in your browser with the following hyperlink changed with yourIP address:
http://YOUR.IP.ADD.RESS/info.php
Congratulations! You have just installed LEMP on your CentOS 6.7 Server. Thank you for following along in this How-To! Check back with us for any new updates.
How To Set Up nginx Virtual Hosts
Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks
Source:
- digitalocean.com
- tecmint.com
- atlantic-community
- github.com
[lastupdated]
.