美文网首页
LNMP 搭建

LNMP 搭建

作者: DB哥 | 来源:发表于2019-08-27 19:57 被阅读0次

Linux System Environment

[root@master01 ~]# cat /etc/redhat-release                  #==》系统版本
CentOS Linux release 7.5.1804 (Core)
[root@master01 ~]# uname –r                             #==》系统内核
3.10.0-862.el7.x86_64
[root@master01 ~]# uname -m                             #==》系统位数
x86_64
[root@master01 ~]#echo $LANG                            #==》系统字符集
en_US.UTF-8
[root@test ~]# nginx -v
nginx version: nginx/1.16.1                                 #==》nginx版本
[root@test ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.27, for Linux (x86_64) using  EditLine wrapper#==》mysql版本
[root@test ~]# php -v
PHP 7.1.31 (cli) (built: Aug  4 2019 09:25:59) ( NTS )          #==》php版本

什么是LNMP?

LNMP是一种网站架构,L代表Linux,N代表Nginx,M代表MySQL,P代表PHP

LNMP工作流程图

用户访问LNMP流程

1、用户如果访问静态页面资源,通过http协议发起请求,Nginx返回资源
2、用户如果访问动态页面资源,通过http协议发起请求
3、Nginx通过fast_pass参数把用户动态请求转发给PHP服务的php-fpm进程处理
4、php-fpm进程再把动态请求转发给PHP服务wrapper进程处理
5、wrapper把接收到的请求进行解析,如果是解析代码直接返回结果
6、如果有查询数据库的操作,则wrapper进程会把结果转发给MySQL处理
7、MySQL把处理的结果直接返回给wrapper在返回给php-fpm最后返回给用户
8、最终数据 user    http  nginx  fastcg i php-fpm  php  mysql

一、配置阿里云yum源

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum makecache

二、创建nginx虚拟用户为Nginx和PHP统一的虚拟用户
标注:nginx虚拟用户可以自定义

[root@test ~]# useradd -s /sbin/nologin -M nginx
[root@test ~]# id nginx
uid=1002(nginx) gid=1002(nginx) groups=1002(nginx)

三、安装Nginx(使用Nginx官方源Yum安装)

1、配置Nginx官方仓库源

[root@nginx02 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

2、yum安装

[root@test ~]# yum -y install nginx

3、修改Nginx服务虚拟用户为nginx

[root@test ~]# vim /etc/nginx/nginx.conf 
user  nginx;

4、启动Nginx并设置开机自启动

[root@test ~]# systemctl start nginx
[root@test ~]# systemctl enable nginx
[root@test ~]# systemctl status nginx

四、安装PHP(使用第三方源Yum安装)

1、yum安装PHP


[root@test ~]#  yum remove php-mysql-5.4 php php-fpm php-common
[root@test ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@test ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@test ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

2、修改PHP服务虚拟用户为nginx

[root@test ~]# vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx

3、启动php-fpm并设置开机自启动

[root@test ~]# systemctl start php-fpm
[root@test ~]# systemctl enable php-fpm
[root@test ~]# systemctl status php-fpm

五、安装MySQL(使用Yum安装)

1、配置MySQL官方源,开启MySQL 5.7版本yum源,关闭MySQL 8.0版本的yum源
MySQL Yum安装说明https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/

[root@test yum.repos.d]# rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
[root@test yum.repos.d]# yum-config-manager --enable mysql57-community
[root@test yum.repos.d]# yum-config-manager --disable mysql80-community
[root@test yum.repos.d]#  yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community   disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community   disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community   disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - disabled
mysql-connectors-community/x86_64  MySQL Connectors Community    enabled:    118
mysql-connectors-community-source  MySQL Connectors Community -  disabled
mysql-tools-community/x86_64       MySQL Tools Community         enabled:     95
mysql-tools-community-source       MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64         MySQL Tools Preview           disabled
mysql-tools-preview-source         MySQL Tools Preview - Source  disabled
mysql55-community/x86_64           MySQL 5.5 Community Server    disabled
mysql55-community-source           MySQL 5.5 Community Server -  disabled
mysql56-community/x86_64           MySQL 5.6 Community Server    disabled
mysql56-community-source           MySQL 5.6 Community Server -  disabled
mysql57-community/x86_64           MySQL 5.7 Community Server    enabled:    342
mysql57-community-source           MySQL 5.7 Community Server -  disabled
mysql80-community/x86_64           MySQL 8.0 Community Server    disabled
mysql80-community-source           MySQL 8.0 Community Server -  disabled

2、yum安装MySQL 5.7数据库

[root@test ~]# yum –y install mysql-community-server

3、启动MySQL并开机自启动

[root@test ~]# systemctl start mysqld
[root@test ~]# systemctl enable mysqld
[root@test ~]# systemctl status mysqld

4、修改MySQL root用户密码
标注:MySQL 5.7版本安装完后会自动创建一个默认密码

[root@test ~]# grep 'temporary password' /var/log/mysqld.log
2019-08-27T01:28:24.736943Z 1 [Note] A temporary password is generated for root@localhost: ::2s:vg:46hF
[root@test ~]# mysql -uroot -p'::2s:vg:46hF'
#==》提示密码复杂度不够
mysql> alter user 'root'@'localhost' identified by 'AAAaaa111';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user 'root'@'localhost' identified by '#AAAaaa111#';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

六、LNMP环境配置

1、确认Nginx / PHP / MySQL 都启动

[root@test ~]# systemctl status nginx
[root@test ~]# systemctl status php-fpm
[root@test ~]# systemctl status mysqld
[root@test ~]# netstat -tlunp

2、测试nginx

[root@test ~]# curl -I 10.0.0.100
[root@test ~]# curl -I -s -w "%{http_code}\n" -o /dev/null  10.0.0.100
200

3、测试PHP

[root@test ~]# vim /etc/nginx/conf.d/www.conf
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

     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;
    }

}
[root@test ~]# systemctl restart nginx
[root@test ~]# curl -I 10.0.0.100/index.php
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Tue, 27 Aug 2019 02:24:02 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.1.31

4、测试MySQL数据库

[root@test ~]# mysql -uroot -p'#AAAaaa111#' -e "select user,host from mysql.user;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-----------+
| user                  | host           |
+---------------+-----------+
| mysql.session         | localhost  |
| mysql.sys             | localhost  |
| root                  | localhost  |
+---------------+-----------+

5、测试PHP访问MySQL数据库是否正常

[root@test ~]# vim /usr/share/nginx/html/testmysql.php
<?php
    $servername = "localhost";
    $username = "root";
    $password = "#AAAaaa111#";

    // 创建连接
    $conn = mysqli_connect($servername, $username, $password);

    // // 检测连接
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "php 连接 MySQL 数据库成功";
?>

[root@test ~]# curl 10.0.0.100/testmysql.php
php 连接 MySQL 数据库成功

七、部署博客网站wordpress

1、配置Nginx虚拟主机站点

[root@test ~]# cp /etc/nginx/conf.d/www.conf /etc/nginx/conf.d/blog.www
[root@test ~]# vim /etc/nginx/conf.d/blog.conf
server {
    listen       80;
    server_name  www.xiaozou.com;
    root   /www/blog/wordpress;
    index  index.html index.htm index.php;

     location ~ \.php$ {
        root /www/blog/wordpress;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

2、重启 Nginx服务

[root@test ~]# systemctl restart nginx

3、下载并解压wordpress程序包并授权nginx虚拟用户权限

[root@test ~]# mkdir -p /www/blog
[root@test ~]# wget -P /www/blog https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@test ~]# tar xf /www/blog/wordpress-4.9.4-zh_CN.tar.gz
[root@test ~]# tar xf /www/blog/wordpress-4.9.4-zh_CN.tar.gz -C /www/blog/
[root@test ~]# chown -R nginx.nginx /www/blog/wordpress

4、MySQL创建wordpress库并授权wp用户访问wordpress库

[root@test ~]# mysql -uroot -p'#AAAaaa111#'
mysql> show databases;
mysql> create database wordpress;
mysql> grant all privileges on wordpress.* to 'wp'@'localhost' identified by '#BBBbbb111#';
mysql> flush privileges;

5、浏览器访问wordpress并部署

在windows系统hosts文件C:\Windows\System32\drivers\etc\hosts添加如下内容
10.0.0.100   www.xiaozou.com

相关文章

网友评论

      本文标题:LNMP 搭建

      本文链接:https://www.haomeiwen.com/subject/gcjrectx.html