LNMP搭建

一、LNMP搭建

1.官方源安装nginx

1)配置官方源

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

#安装nginx
[root@web01 ~]# yum install -y nginx

2.设置统一用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666

3.修改nginx配置文件

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

4.启动服务并验证

[root@web01 ~]# systemctl start nginx

#设置开机自启
[root@web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

#验证
[root@web01 ~]# ps -ef | grep nginx

5.安装php (7版本)

1)配置第三方源

[root@web01 ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

2)卸载旧版本

[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common

3)安装php

1>安装方式一

#Centos7 安装源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

[root@web01 ~]# 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>安装方式二

#创建存放服务包的目录
[root@web01 ~]# mkdir /package
[root@web01 ~]# cd /package/

#上传包
[root@web01 /package]# rz php.tar.gz

#解压包
[root@web01 /package]# tar xf php.tar.gz

#安装本地所有rpm包
[root@web01 /package]# yum localinstall -y *.rpm

4)配置PHP

#修改用户为统一
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
user = www
group = www

5.)启动服务并验证

#启动服务
[root@web01 /package]# systemctl start php-fpm

#开启自启
[root@web01 /package]# systemctl enable php-fpm

#验证服务是否启动
[root@web01 /package]# ps -ef | grep php

#查看端口
[root@web01 /package]# netstat -lntp | grep php
tcp       0      0 127.0.0.1:9000         0.0.0.0:*        LISTEN      7748/php-fpm: master

6.搭建交作业页面

1)配置nginx

[root@web01 /package]# vim /etc/nginx/conf.d/zuoye.conf 
server {
    listen 80;
    server_name www.zuoye.com;

    location / {
        root /code/zuoye;
        index index.html;
    }
}

#创建站点目录
[root@web01 /package]# mkdir /code/zuoye -p

#上传代码
[root@web01 /package]# cd /code/zuoye/
[root@web01 /code/zuoye]# rz kaoshi.zip
[root@web01 /code/zuoye]# unzip kaoshi.zip

#修改代码内的存放文件路径
[root@web01 /code/zuoye]# vim upload_file.php 
$wen="/code/zuoye/upload";

#授权
[root@web01 /code/zuoye]# chown -R www.www /code/

2)访问测试

#重启nginx服务
[root@web01 /code/zuoye]# systemctl restart nginx

#配置本地C:\Windows\System32\drivers\etc\hosts文件
10.0.0.7 www.zuoye.com

#浏览器访问www.zuoye.com

3)报错

报错405,原因是nginx作为web服务器没有办法处理post请求,我们要用php的代码,需要关联nginx和php
在这里插入图片描述

7.关联nginx和php

1)关联语法

#设置FastCGI服务的地址。该地址可以指定为域名或IP地址以及端口:
Syntax:	fastcgi_pass address;
Default:	—
Context:	location, if in location


#设置参数应传递给FastCGI服务器的。该value可以包含文件,变量
Syntax:	fastcgi_param parameter value [if_not_empty];
Default:	—
Context:	http, server, location

fastcgi_params文件详解

2)完整配置

[root@web01 /code/zuoye]# vim /etc/nginx/conf.d/default.conf 
server {
    listen 80;
    server_name www.zuoye.com;

    location / {
        root /code/zuoye;
        index index.html;
    }

    location ~* \.php$ {   
    	#设置FastCGI服务地址
        fastcgi_pass 127.0.0.1:9000;   
        #当访问/code/zuoye/index.php的时候,需要读取网站目录下面的.php文件,如果没有配置这一配置项时,nginx不会去网站目录下访问.php文件,所以返回空白
        fastcgi_param SCRIPT_FILENAME /code/zuoye/$fastcgi_script_name;
        include fastcgi_params;
    }
}

3)访问页面测试(修改上传文件大小限制)

#浏览器访问www.zuoye.com
1.上传文件成功

2.413报错:文件过大,解决方式
#修改nginx上传文件大小
[root@web01 /code/zuoye]# vim /etc/nginx/nginx.conf
http {
	... ...
    client_max_body_size 100M;
    ... ...
}
[root@web01 /code/zuoye]# systemctl restart nginx

#修改php上传文件大小
[root@web01 /code/zuoye]# vim /etc/php.ini
post_max_size = 100M
upload_max_filesize = 100M
[root@web01 /code/zuoye]# systemctl restart php-fpm

8.搭建mariadb(mysql)

1)安装

#安装
[root@web01 /code/zuoye]# yum install -y mariadb-server
#安装不了需删除之前配置的php.repo
[root@web01 ~]# rm -rf /etc/yum.repos.d/php.repo

#开启并添加自启
[root@web01 /code/zuoye]# systemctl start mariadb
[root@web01 /code/zuoye]# systemctl enable mariadb

#验证
[root@web01 /code/zuoye]# netstat -lntp | grep mysql
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      9887/mysqld

2)连接

[root@web01 /code/zuoye]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;   #显示数据库
+--------------------+
| Database           |     
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

3)设置数据库密码

#设置密码
[root@web01 /code/zuoye]# mysqladmin -u root password "123"

#使用密码连接
[root@web01 /code/zuoye]# mysql -u root -p
Enter password:

9.关联php和MySQL

1)编写php测试连接数据库代码

[root@web01 /code/zuoye]# vim php_mysql.php
<?php
    $servername = "localhost";
    $username = "root";
    $password = "123";

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

    // 检测连接
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "小哥哥,php可以连接MySQL...";
?>

<img style='100%;height:100%;' src=https://blog.driverzeng.com/zenglaoshi/php_mysql.png>


二、LNMP架构搭建wordpress

1.上传代码

#上传包
[root@web01 /code]# rz wordpress-5.0.3-zh_CN.tar.gz

#解压
[root@web01 /code]# tar xf wordpress-5.0.3-zh_CN.tar.gz

#授权
[root@web01 /code]# chown -R www.www /code

2.配置conf文件

#查看网站首页文件格式
[root@web01 /code]# ll wordpress
total 192
-rw-r--r--  1 www www   418 Sep 25  2013 index.php

[root@web01 /code]# cd /etc/nginx/conf.d

[root@web01 /etc/nginx/conf.d]# vim www.blog.com.conf
server {
    listen 80;
    server_name www.blog.com;

    location / {
        root /code/wordpress;
        index index.php;
    }

    location ~* \.php$ {
        root /code/wordpress;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

#重启服务
[root@web01 /etc/nginx/conf.d]# systemctl restart nginx

3.访问网页

#配置本地C:\Windows\System32\drivers\etc\hosts文件
10.0.0.7 www.blog.com

#访问
www.blog.com

4.创建数据库

[root@web01 /etc/nginx/conf.d]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#创建数据库
MariaDB [(none)]> create database blog;
Query OK, 1 row affected (0.00 sec)

#查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| blog               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

MariaDB [(none)]> quit
Bye


三、LNMP搭建知乎

1.上传代码

[root@web01 /code]# rz WeCenter_3-2-1.zip
[root@web01 /code]# unzip WeCenter_3-2-1.zip
[root@web01 /code]# mv WeCenter_3-2-1 zhihu

#授权
[root@web01 /code]# chown -R www.www /code/

2.配置

[root@web01 /code]# vim /etc/nginx/conf.d/www.zh.com.conf
server {
    listen 80;
    server_name www.zh.com;

    location / {
        root /code/zhihu;
        index index.php;
    }

    location ~* \.php$ {
        root /code/zhihu;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

3.重启访问

[root@web01 /code]# systemctl restart nginx

#配置本地hosts
www.zh.com

4.创建数据库

[root@web01 /code]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 131
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database zh;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| blog               |
| mysql              |
| performance_schema |
| test               |
| zh                 |
+--------------------+
6 rows in set (0.00 sec)
原文地址:https://www.cnblogs.com/backz/p/15548368.html