企业实战LNMP高性能服务器:
- LNMP WEB架构中,Nginx为一款高性能Web服务器,本身是不能处理PHP的,当接收到客户端浏览器发送HTTP Request请求时,Nginx服务器响应并处理web请求,静态资源CSS、图片、视频、TXT等静态文件请求,Nginx服务器可以直接处理并回应。
- 但是PHP动态页面请求Nginx不能直接处理,Nginx服务器会将PHP网页脚本通过接口传输协议(网关协议)PHP-FCGI(Fast-CGI)传输给PHP-FPM(进程管理程序),PHP-FPM不做处理,然后PHP-FPM调用PHP解析器进程,PHP解析器解析PHP脚本信息。PHP解析器进程可以启动多个,可以实现多进行并发执行。
- PHP解释器将解析后的脚本返回到PHP-FPM,PHP-FPM再通过Fast-CGI的形式将脚本信息传送给Nginx,Nginx服务器再通过Http Response的形式传送给浏览器,浏览器再进行解析与渲染然后进行呈现。
1. 安装MySQL:
[root@localhost ~]# yum -y install mysql-community-server
[root@localhost ~]# systemctl start mysqld
注意:这里必须配置好MySQL yum源,才可执行以上操作,可以参考怎么用yum安装MySQL
2. 安装nginx:
[root@localhost ~]# yum -y install nginx
[root@localhost ~]# systemctl start nginx
注意:这里需要配置好nginx yum源才可以执行,可以参考nginx的安装
3. 安装php:
3.1. 安装依赖包:
[root@localhost ~]# yum -y install gcc gd curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel
3.2. 下载php源码包:
[root@localhost ~]# wget https://www.php.net/distributions/php-5.6.8.tar.bz2
3.3. 解压php源码包:
[root@localhost ~]# tar -xf php-5.6.8.tar.bz2 -C /usr/src/
[root@localhost ~]# cd /usr/src/php-5.6.8/
3.4. 预编译:
[root@localhost php-5.6.8]# ./configure --prefix=/usr/local/php \
--enable-fpm \
--enable-debug \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--enable-mbstring \
--with-curl \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-config-file-path=/usr/local/php/etc \
--with-zlib-dir
3.5. 编译&&安装:
[root@localhost php-5.6.8]# make && make install
[root@localhost php-5.6.8]# cp php.ini-development /usr/local/php/etc/php.ini
[root@localhost php-5.6.8]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-5.6.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.6.8]# chmod +x /etc/init.d/php-fpm
3.6. 修改php-fpm的用户:
[root@localhost php-5.6.8]# vim /usr/local/php/etc/php-fpm.conf
修改以下内容:
user = nginx
group = nginx
3.7. 启动php-fpm:
[root@localhost ~]# /etc/init.d/php-fpm start
4. 下载门户网站文件:
[root@localhost ~]# wget https://files.phpmyadmin.net/phpMyAdmin/4.9.5/phpMyAdmin-4.9.5-all-languages.zip
[root@localhost ~]# wget https://wordpress.org/wordpress-4.9.4.zip
[root@localhost ~]# unzip phpMyAdmin-4.9.5-all-languages.zip -d /usr/share/nginx/html/
[root@localhost ~]# unzip wordpress-4.9.5.zip -d /usr/share/nginx/html/
[root@localhost ~]# chown nginx. -R /usr/share/nginx/html/wordpress/
[root@localhost ~]# chown nginx. -R /usr/share/nginx/html/phpMyAdmin-4.9.5-all-languages/
5. 创建虚拟主机:
修改配置文件如下:
[root@localhost ~]# vim /etc/nginx/nginx.conf
include /etc/nginx/vhost/*.conf;
创建虚拟主机配置文件:
[root@localhost ~]# mkdir /etc/nginx/vhost
[root@localhost ~]# vim /etc/nginx/vhost/blog.wordpress.com.conf
server {
listen 80;
server_name blog.wordpress.com;
charset utf-8;
access_log /var/log/nginx/wordpress.access.log main;
location / {
root /usr/share/nginx/html/wordpress;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@localhost html]# vim /etc/nginx/vhost/www.boke.com.conf
server {
listen 80;
server_name www.boke.com;
charset utf-8;
access_log /var/log/nginx/boke.access.log main;
location / {
root /usr/share/nginx/html/phpMyAdmin-4.9.5-all-languages;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html/phpMyAdmin-4.9.5-all-languages;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
6. 创建数据库:
[root@localhost ~]# vim /etc/my.cnf
在mysqld指令块输入以下内容:
validate_password_policy=0
validate_password_length=4
重启服务:
[root@localhost ~]# systemctl restart mysqld
进入MySQL:
[root@localhost ~]# grep password /var/log/mysqld.log
2020-03-21T22:18:16.434127Z 1 [Note] A temporary password is generated for root@localhost: pWpa,pBJa21B
[root@localhost ~]# mysql -uroot -ppWpa,pBJa21B
必须修改初始密码才可以操作其他内容:
mysql> alter user 'root'@'localhost' identified by '1234';
mysql> create database phpMyAdmin charset=utf8;
Query OK, 1 row affected (0.00 sec)
mysql> create database wordpress charset=utf8;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on phpMyAdmin.* to 'phpMyAdmin'@'localhost' identified by '1234';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on wordpress.* to 'wordpress'@'localhost' identified by '1234';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit;
修改phpMyAdmin文件内容:
[root@localhost ~]# vim /usr/share/nginx/html/phpMyAdmin-4.9.2-all-languages/libraries/config.default.php
修改以下内容:
* @global string $cfg['Servers'][$i]['host']
*/
$cfg['Servers'][$i]['host'] = '127.0.0.1'; 默认是localhost修改为127.0.0.1
7. 修改Windows系统的hosts文件:
路径:C:\Windows\System32\drivers\etc\hosts
修改如下:
192.168.2.10 www.boke.com bolg.wordpress.com
验证:
www.boke.com
blog.wordpress.com