LEMP or a.k.a LNMP stands for Linux, Nginx, MySQL and PHP. I posted
various tutorial on how to get it installed on your VPS but this article
is a CentOS 7 version of how to setup a full and working Nginx server
to host websites or blogs on your VPS.
Just like my Apache-version of this tutorial I’ll use MariaDB to act as mysql server instead of the original one which is now owned by Oracle. For your information, MariaDB is an enhanced, drop-in replacement for MySQL made by its original developers. For all practical purposes, MariaDB is a binary drop in replacement of the same MySQL version (for example MySQL 5.1 -> MariaDB 5.1, MariaDB 5.2 & MariaDB 5.3 are compatible. Shortly, if your apps / scripts can run on MySQL, then it will also run flawlessly on MariaDB.
Step 1 – Login to your server via Putty or Terminal.
Step 2 – Since Nginx package is not included in CentOS 7 by default, so we have to add / install Nginx yum repository by issuing command below:
it will look like this
Step 3 – Next, simply use this simple command to install Nginx on CentOS 7:
and once done, it should look similar to this:
As simple as that! Now your Nginx web server is running.
Step 4 – You can test Nginx to run for the very first time using this command:
and enable it to automatically run each time your server is booting:
Now open up your favorite web browser and use your server’s IP to access it. Nginx default welcome page should be seen there:
You can use nginx -v command to find out what version of Nginx is installed:
That’s a very basic command to install PHP 5 plus its modules:
php-mysql and php-fpm. However, in many cases your apps or scripts (like
WordPress and its many plugins) require other PHP modules. You can list
all available module using yum search php- (read here). I’ve collected all most common modules you may need so you can simply use this one:
once done, you’ll see something like this:
Step 6 – As we’ll use php-fpm module, so there are few configuration you have to adjust. First, let’s edit php.ini file using your favorite text editor like Nano:
then look for the line to with cgi.fix_pathinfo. In Nano you can make use of search function (Control+W). Remove the ; and set this value to 0.
Before:
After:
Once done, save and exit the editor (Control+O then Control+X)
Step 7 – Next, edit php-fpm configuration file:
and edit the line Listen = 127.0.0.1:9000 replace whit this:
Step 8 – Also find the section called “Unix user/group of processes” then change Apache to Nginx:
Done editing? Now save and exit.
Step 9 – Now start php-fpm using systemctl command:
and enable it to automatically start on system boot:
It should look like this by default:
Step 11 – Now add an index.php option as the first value of index directive to allow PHP index files to be served when a directory is requested:
Also change the structure from this :
to this:
Step 12 – Also change the server_name directive to point to your server’s domain name or public IP address:
Step 13 – Optional but necessary, uncomment some lines that define error processing routines especially error 404:
Step 14 – Then scroll down a bit and find a section that says “pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000” uncomment the lines and make changes as following:
Done? You can now save and exit.
Step 15 – Do not forget to restart Nginx so the changes can be applied:
Step 16 – Test that Nginx and PHP-fpm can actually process any php script. For this purpose you can simply create a info.php page:
Next, open up your browser and try open that newly created info.php file which looks like this:
Step 18 -Start MariaDB service using systemctl command (get used to it):
and enable it on boot:
Step 19 – So its service is now running but there is one thing you should do immediately: configuring MariaDB setup for the very first time like setting up your mysql root password. Issue this command:
Then you’ll see a series of question, just answer it accordingly. The
main important part is to define your root password while everything
else is just up to you or you can simply hit the “ENTER” key through
each prompt to accept the default values.
Step 20 – You may also need to test your newly installed MariaDB by logging in as root:
As you can see from the screenshot above, it is Maria DB v5.5.37.
The url above is still its beta version. However if the repository is out of beta status, the link most likely will be different. In case that happens, you can find out its latest download url at Fedora Project website.
Step 22 – Now use yum to install phpmyadmin:
Step 23 – In order to make PhpMyAdmin accessible to the web, you have to create a symbolic link from the PMA installation files to your Nginx document root directory using this command:
now restart php-fpm:
Step 24 – That’s it. Now you can test it on your browser:
http://ip-or-domain/phpMyAdmin
and default login page of phpMyAdmin should be displayed:
Just like my Apache-version of this tutorial I’ll use MariaDB to act as mysql server instead of the original one which is now owned by Oracle. For your information, MariaDB is an enhanced, drop-in replacement for MySQL made by its original developers. For all practical purposes, MariaDB is a binary drop in replacement of the same MySQL version (for example MySQL 5.1 -> MariaDB 5.1, MariaDB 5.2 & MariaDB 5.3 are compatible. Shortly, if your apps / scripts can run on MySQL, then it will also run flawlessly on MariaDB.
L for Linux
CentOS 7 is one of available most recent Linux distro. It has great enhancements over its predecessor. Well, there are few changes but to note the most important one, it is now using systemd, the replacement for legacy System V (SysV) startup scripts and runlevels. With systemd there are fewer files to edit, and all the services are compartmentalized and stand separate from each other. Shortly saying, instead of using /etc/init.d/httpd restart or service httpd restart, you should now make yourself be familiar with systemctl restart httpd.service command.E for Engine X or N for Nginx
You must already knew it, Nginx is most popular web server with Apache’s twice performance. It is an open source reverse proxy server for HTTP, HTTPS protocols, as well as a load balancer, HTTP cache, and a web server (origin server). Itaccelerates content and application delivery, improves security, facilitates availability and scalability for the busiest web sites on the Internet. Installing Nginx is easy but building a website with high traffic capable to crash Nginx is the hardest part.M for MySQL
MySQL is a database server but in this tutorial we’ll use MariaDB.P for PHP
Hypertext Preprocessor or PHP is a widely-used open source general-purpose scripting language. On websites, it is useful to serve dynamic content.Prerequisite
- A server (VPS / Dedi) running CentOS 7. I recommend you to use CentOS 7 x86_64 minimal if available. As usual, I’m using a Playground Server from DigitalOcean.
- A knowledge on how to use Putty or Terminal to access a server via SSH.
- I believe you knew –at least part of– most common Unix commands used to manage an unmanaged server.
- A spare time of your life and a cup of coffee.
Install Nginx
Before you proceed to the next steps, it is better to explain that all commands in this tutorial are written without the “sudo” prefix. In this tutorial I use root but you may also login as separate user with root privilege. However if you disabled root login and you logged in using another username with root privilege, you can add the “sudo” prefix all by your self. Alternatively you can simply type su, hit Enter and type in your password twice to switch as root.Step 1 – Login to your server via Putty or Terminal.
Step 2 – Since Nginx package is not included in CentOS 7 by default, so we have to add / install Nginx yum repository by issuing command below:
1
| rpm -Uvh http: //nginx .org /packages/centos/7/noarch/RPMS/nginx-release-centos-7-0 .el7.ngx.noarch.rpm |
Step 3 – Next, simply use this simple command to install Nginx on CentOS 7:
1
| yum install nginx -y |
and once done, it should look similar to this:
As simple as that! Now your Nginx web server is running.
Step 4 – You can test Nginx to run for the very first time using this command:
1
| systemctl start nginx.service |
1
| systemctl enable nginx.service |
Now open up your favorite web browser and use your server’s IP to access it. Nginx default welcome page should be seen there:
You can use nginx -v command to find out what version of Nginx is installed:
Install PHP5-fpm
Step 5 – PHP can be easily installed via yum:
1
| yum install php php-mysql php-fpm |
1
| yum install php php-fpm php-common php-cli php-devel php-gd php-imap php-intl php-mysql php-process php-xml php-xmlrpc php-zts -y |
Step 6 – As we’ll use php-fpm module, so there are few configuration you have to adjust. First, let’s edit php.ini file using your favorite text editor like Nano:
1
| nano /etc/php .ini |
Before:
After:
Once done, save and exit the editor (Control+O then Control+X)
Step 7 – Next, edit php-fpm configuration file:
1
| nano /etc/php-fpm .d /www .conf |
1
| listen = /var/run/php-fpm/php-fpm .sock |
Step 8 – Also find the section called “Unix user/group of processes” then change Apache to Nginx:
Done editing? Now save and exit.
Step 9 – Now start php-fpm using systemctl command:
1
| systemctl start php-fpm |
1
| systemctl enable php-fpm.service |
Setup Nginx (Configuration)
Step 10 – Open and edit the default Nginx server block (virtual hosts file) configuration file using your favorite editor:
1
| nano /etc/nginx/conf .d /default .conf |
Step 11 – Now add an index.php option as the first value of index directive to allow PHP index files to be served when a directory is requested:
Also change the structure from this :
to this:
Step 12 – Also change the server_name directive to point to your server’s domain name or public IP address:
Step 13 – Optional but necessary, uncomment some lines that define error processing routines especially error 404:
Step 14 – Then scroll down a bit and find a section that says “pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000” uncomment the lines and make changes as following:
1
2
3
4
5
6
7
| location ~ \.php$ { try_files $uri =404; fastcgi_pass unix: /var/run/php-fpm/php-fpm .sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } |
Done? You can now save and exit.
Step 15 – Do not forget to restart Nginx so the changes can be applied:
1
| systemctl restart nginx.service |
1
| echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info .php |
Install MariaDB
Step 17 – You have Nginx and PHP-fpm up and running so it is time to install MariaDB server. The command is simple and done via yum:
1
| yum install mariadb-server mariadb -y |
Step 18 -Start MariaDB service using systemctl command (get used to it):
1
| systemctl start mariadb.service |
1
| systemctl enable mariadb.service |
Step 19 – So its service is now running but there is one thing you should do immediately: configuring MariaDB setup for the very first time like setting up your mysql root password. Issue this command:
1
| mysql_secure_installation |
Step 20 – You may also need to test your newly installed MariaDB by logging in as root:
1
| mysql -u root -p |
As you can see from the screenshot above, it is Maria DB v5.5.37.
Install PhpMyAdmin
Step 21 – Unluckily, this PhpMyadmin package is not available in CentOS 7.0 default repositories. In this case you have to add / enable third-party repo like EPEL. Simply issue this command to do that:
1
2
3
| wget http: //download .fedoraproject.org /pub/epel/beta/7/x86_64/epel-release-7-0 .2.noarch.rpm rpm -ivh epel-release* rm epel-release* |
The url above is still its beta version. However if the repository is out of beta status, the link most likely will be different. In case that happens, you can find out its latest download url at Fedora Project website.
Step 22 – Now use yum to install phpmyadmin:
1
| yum install phpmyadmin -y |
Step 23 – In order to make PhpMyAdmin accessible to the web, you have to create a symbolic link from the PMA installation files to your Nginx document root directory using this command:
1
| ln -s /usr/share/phpMyAdmin /usr/share/nginx/html |
1
| systemctl restart php-fpm.service |
Step 24 – That’s it. Now you can test it on your browser:
http://ip-or-domain/phpMyAdmin
and default login page of phpMyAdmin should be displayed:
No comments:
Post a Comment