Home >php教程 >PHP视频 >Tutorial on setting up LNMP environment under centos 7.2

Tutorial on setting up LNMP environment under centos 7.2

高洛峰
高洛峰Original
2016-12-12 09:48:131741browse

Local environment: The server is Alibaba Cloud ECS; the image used is: public image CENTOS 7.2

一.nginx installation

1. Download the nginx package corresponding to the current system version

wget http://nginx .org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2. Create nginx yum warehouse (the default yum does not have nginx)

rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm

3. Download and install nginx

yum install nginx

4.nginx starts (under the nginx installation directory-/usr/ sbin/)

systemctl start nginx.service

ps: Some other nginx related commands:

nginx related configuration files:

The default configuration file is under the /etc/nginx path, and it can run correctly using this configuration nginx;

If you need to customize it, just modify the nginx.conf and other files under it;

Enter the IP of the machine where the nginx environment is deployed in the browser address bar. If everything is normal, you should see the following content .

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer tonginx.org.Commercial support is available atnginx.com.

Thank you for using nginx.

Step 2: MYSQL installation

1. First download the repo source of mysql; related commands:

wget http://repo.mysql.com/mysql-community-release- el7-5.noarch.rpm

2. Install the mysql-community-release-el7-5.noarch.rpm package

(After installing this package, you will get two mysql yum repo sources: /etc/yum. repos.d/mysql-community.repo, /etc/yum.repos.d/mysql-community-source.repo)

rpm -ivh mysql-community-release-el7-5.noarch.rpm

3. Install MYSQL

sudo yum install mysql-server

4. Reset password

Change user permissions:

sudo chown -R root:root /var/lib/mysql

Restart the service:

systemctl restart mysql .service

Log in and change the password:

mysql -u root
 
mysql > use mysql;
 
mysql > update user set password=password(‘123456‘) where user=‘root‘;
 
mysql > exit;

Step 3: Install php

1. Check the currently installed php version (yum list installed | grep php)

If there is a php installation package First delete the previous version and use yum remove to remove php related packages

2.rpm Install Php7 corresponding yum source

rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
 
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

3. Install php7.0

yum install php70w

4. Install php extension

php70w-mysql.x8664 mysql extension (install php70w-pdo.x8664 as a dependency)

php70w-gd.x86_64 GD library is an extension library for PHP to process graphics. The GD library provides a series of APIs for processing images. , use the GD library to process images or generate images.

php70w-ldap.x86_64 "Lightweight Directory Access Protocol" is a protocol used to access "Directory Servers";

php70w-mbstring.x86_64 mbstring extension library is used to process multi-byte strings , PHP's built-in string length function strlen cannot correctly handle Chinese strings. It only gets the number of bytes occupied by the string. For GB2312 Chinese encoding, the value obtained by strlen is twice the number of Chinese characters, while for UTF-8 encoded Chinese, the difference is 1 to 3 times. This problem can be better solved by using the mb_strlen function.

php70w-mcrypt.x86_64 The Mcrypt extension library can realize encryption and decryption functions, that is, it can not only encrypt plain text, but also restore cipher text.

5. Install PHP FPM

yum install php70w-fpm

About php-fpm

PHP-FPM is actually a patch of PHP source code, designed to integrate FastCGI process management into the PHP package. It must be patched into your PHP source code and can be used after compiling and installing PHP.

Now we can download the branch that directly integrates PHP-FPM in the source tree of the latest PHP 5.3.2. It is said that the next version will be integrated into the main branch of PHP. Compared with Spawn-FCGI, PHP-FPM has better CPU and memory control, and the former is easy to crash and must be monitored with crontab, while PHP-FPM does not have such troubles.

PHP5.3.3 has integrated php-fpm and is no longer a third-party package. PHP-FPM provides a better PHP process management method, which can effectively control memory and processes, and can smoothly reload PHP configuration. It has more advantages than spawn-fcgi, so it is officially included in PHP. You can enable PHP-FPM by passing the –enable-fpm parameter in ./configure.

Use PHP-FPM to control the FastCGI process of PHP-CGI

Step 4: Configure nginx

Remember to back up before modifying the configuration file

1.nginx configuration file location: (/etc/nginx/conf.d/ default.conf)

Modify the web root directory (you don’t need to modify it if you don’t need it, just use the default):

root /wwwdata/html;

Configure PHP parsing and modify the bold black part in the code below:

location ~.php$ {
 
fastcgi_pass 127.0.0.1:9000;
 
fastcgi_index index.php;
 
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 
include    fastcgi_params;
 
}

2.php-fpm configuration file location: (/etc/php-fpm.d/www.conf)

Modify

user =nginx

group=nginx

3. Start nginx service :

systemctl start nginx.service

If you need to set up auto-start at boot, use the following command:

sudo systemctl enable nginx.service

Check the startup status:

systemctl status nginx

See the following words to indicate that the startup is successful!

Active: active (running) since Saturday 2016-11-19 13:40:04 CST; 50min ago

4. Start PHP-FPM:

systemctl start php-fpm.service

If you need to set the startup auto Use the following command to start:

sudo systemctl enable php-fpm.service

Check the startup status:

systemctl status php-fpm.service

Seeing the following words indicates that the startup is successful!

Active: active (running) since Saturday 2016-11-19 14:14:33 CST; 18min ago

At this point, the php+mysql+nginx environment is set up!

Finally, test the next php file in the web root directory; hoo~~

In order to access and modify the database more conveniently, you need to install the mysql management tool phpMyAdmin

Jump to the web root directory: (Jump according to the previous settings Your own website root directory)

cd /wwwdata/html/

Download the pma installation package:

wget https://files.phpmyadmin.net/phpMyAdmin/4.4.12/phpMyAdmin-4.4.12-all-languages .zip

Unzip the installation package:

unzip phpMyAdmin-4.4.12-all-languages.zip

If unzip is missing here, you need to install unzip first;

yum install unzip zip

Rename the installation folder

mv phpMyAdmin-4.4.12-all-languages ​​phpmyadmin

Modify the permissions of the session storage directory:

chown -R nginx.nginx /var/lib/php/session

If you need to modify the location of the session root directory, you need to modify the location. : Modify session.save_path in

/etc/php.ini

Since session.save_path in /etc/php-fpm.d/www.conf has a higher priority than php.ini, so if it exists /etc/php- Just modify the session.save_path in fpm.d/www.conf. Visit http://youipaddress/phpmyadin. If you can access it, then you are successful!

Thought that would be enough, but there was another problem logging in! Can't log in!

Made some modifications:

1. Modify the /var/lib/php/session folder permissions to 770 or above

2. Modify $cfg'Servers'['user in config.sample.inc.php in the phpmyadmin directory '] = 'root';$cfg'Servers'['password'] = '123456'; Username and password set for yourself;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more