Rotten mud: learning how to quickly build an LNMP environment for centos, centoslnmp

Source: Internet
Author: User

Rotten mud: learning how to quickly build an LNMP environment for centos, centoslnmp

This document consistsXiuyi Lin FengProviding friendship sponsorship, first launchedThe world

I used to install software in centos in the source code, but yesterday I needed an LNMP environment in centos. Instead, I won't do it. I recorded it for future use.

1. Install nginx

Install nginx first, as shown below:

Yum-y install nginx

We can see that there is no nginx package in the current yum source. Install the yum Source Containing nginx as follows:

Rpm-ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

View the installed rpm package as follows:

Rpm-SQL nginx-release-centos-6-0.el6.ngx.noarch

Through this, we can clearly see that the rpm package just installed only adds a new nginx yum source. View the yum source as follows:

Cat/etc/yum. repos. d/nginx. repo

Now we can use yum to install nginx, as shown below:

Yum-y install nginx

We can see that nginx has been installed.

Now you can view the nginx installation location and its files as follows:

Rpm-ql nginx

We can see that nginx is installed in the/etc/nginx directory by default, while nginx is installed in the/usr/share/nginx/html directory by default.

Now let's start nginx and access nginx. As follows:

/Etc/init. d/nginx start

Curl http: // 192.168.1.124

View the users used during nginx running as follows:

Ps-ef | grep nginx

We can see that nginx is started by the root user, but nginx is used when running.

Ii. install php and php-fpm

Run the following command to install php and php-fpm:

Yum-y install php-fpm

View the php-fpm installation location and its file, as shown below:

Rpm-ql php-fpm

Start php-fpm and run the following command:

/Etc/init. d/php-fpm start

Ps-ef | grep php-fpm

Netstat-tunlp | grep 1355

We can see that apache is used when php-fpm is running, and php-fpm listens to port 9000 of the local machine.

If you want to modify the php-fpm runtime user and port, we can by modifying the php-fpm configuration file/etc/php-fpm.d/www. conf, as shown below:

Cat/etc/php-fpm.d/www. conf | grep-v '^;' | grep-v ^ $

Iii. Integration of nginx and php

Nginx and php are integrated through fastcgi, while fastcgi is usually used by php-fpm. In chapter 2, we have started php-fpm. Now we can modify the nginx configuration file to support php.

Now we modify the default nginx website configuration file default. conf as follows:

Cat/etc/nginx/conf. d/default. conf

Server {

Listen 80;

Server_name localhost;

Location /{

Root/usr/share/nginx/html;

Index. php index.html index.htm;

}

Error_page 500 502 503 x.html;

Location =/50x.html {

Root/usr/share/nginx/html;

}

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;

}

}

After the above configuration is complete, restart nginx and php-fpm, as shown below:

/Etc/init. d/nginx restart

/Etc/init. d/php-fpm restart

The above is the default website for modifying nginx. Now we add a new nginx virtual host and make it support php, as shown below:

Vi/etc/nginx/conf. d/ilanni. conf

Server {

Listen 80;

Server_name test.ilanni.com;

Location /{

Root/ilanni;

Index. php index.html index.htm;

}

Location ~ \. Php $ {

Root/ilanni;

Fastcgi_pass 127.0.0.1: 9000;

Fastcgi_index index. php;

Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;

Include fastcgi_params;

}

}

After the nginx virtual host configuration file is complete, create index. php of the virtual host ilanni as follows:

Mkdir/ilanni

Vi/ilanni/index. php

<? Php phpinfo ();?>

Modify the user of the ilanni directory as follows:

Chown nginx: nginx-R/ilanni/

Ll/ilanni/

As shown in, we now change the ilanni directory to the user nginx used during nginx running.

Now let's modify the php-fpm runtime user as follows:

Vi/etc/php-fpm.d/www. conf

Restart nginx and php-fpm as follows:

/Etc/init. d/nginx restart

/Etc/init. d/php-fpm restart

Through this, we can see that the nginx virtual host can correctly parse php.

4. Install mysql

Run the following command to install MySQL:

Yum-y install mysql-server php-mysql

Start mysql after installation. As follows:

/Etc/init. d/mysqld start

Now we create a mysql. php file on the default nginx website to connect to the mysql database. The content of mysql. php is as follows:

Cat/usr/share/nginx/html/mysql. php

<? Php

$ Host = 'localhost ';

$ Root = 'root ';

$ Pwd = '';

$ Con = mysql_connect ($ host, $ root, $ pwd );

If ($ con = false ){

Echo "connect false ";

} Else {

Echo "connect true ";

}

?>

Restart nginx, php-fpm, and mysql as follows:

/Etc/init. d/nginx restart

/Etc/init. d/php-fpm restart

/Etc/init. d/mysqld restart

Access the mysql. php file as follows:

Http: // 192.168.1.124/mysql. php

We can see that php has Parsed the mysql. php file and successfully connected to the mysql database.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.