1.安装前准备
本文介绍的LNMP环境是在windows的Oracle VM VirtualBox中的Centos虚拟机上搭建的,各个软件的版本为:Centos7 + Nginx + mysql(5.7版) + php(7.1版)。本文是在假设Centos7虚拟机已经安装完成的前提下进行的
#更新
yum update
#安装wget
yum install wget
rpm -qa wget
#手动配置yum源
[root@nginx~]vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=[http://nginx.org/packages/centos/7/$basearch/](https://links.jianshu.com/go?to=http%3A%2F%2Fnginx.org%2Fpackages%2Fcentos%2F7%2F%2524basearch%2F)
gpgcheck=0
enabled=1
原理
LNMP数据传输原理
- 用户通过http协议发起请求,请求会先抵达LNMP架构中的Nginx
- Nginx根据用户的请求进行判断,该判断由Location进行完成(静态请求走本地,动态请求交给后端)。
- 判断用户请求为静态页面,Nginx直接进行处理。
- 判断用户请求的是动态页面,Nginx会将该请求交给fastcgi协议进行下发
- fastcgi协议将请求提交给php-fpm管理进程,php-fpm管理进程接收到后悔生成具体的针对该动态请求的线程warrap。
- 由warrap线程操作php解析器进行解析
- 若请求中设计查询数据库的操作,则由php连接数据库(用户名 密码),然后发起查询操作
- 最终将解析结果返还给用户
2、安装nginx
yum install nginx -y
systemctl start nginx
systemctl enable nginx
#测试打开浏览器nginx是否属于开启状态
3、安装php
#移除旧版php
yum remove php-mysql-5.4 php php-fpm php-common -y
#配置扩展源
yum localinstall -y [http://mirror.webtatic.com/yum/el7/webtatic-release.rpm](https://links.jianshu.com/go?to=http%3A%2F%2Fmirror.webtatic.com%2Fyum%2Fel7%2Fwebtatic-release.rpm)
#安装新的php
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
#统一系统权限,修改php-fpm运行的用户和组身份
sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
#创建用户
useradd www
#启动php-fpm管理进程,并加入开机自启
systemctl start php-fpm
systemctl enable php-fpm
4、测试php是否能与nginx连接
#修改配置文件
[root@chenhuichao /etc/nginx/conf.d]# vim /etc/nginx/conf.d/blog.zhangweibin.conf
server {
listen 80;
server_name blog.zhangweibin.com;
location / {
root /code/test/wordpress;
index index.php index.html;
}
location ~\.php$ {
root /code/test/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
mkdir -p /code/test
[root@chenhuichao /etc/nginx/conf.d]# cat > /code/test/index.php <<EOF> 该文件内容为测试php成功连接
> <?
> phpinfo();
> ?>
> EOF
[root@chenhuichao /etc/nginx/conf.d]# cat /code/test/index.php
该文件内容为测试php成功连接
<?
phpinfo();
?>
nginx -t
systemctl reload nginx
echo "10.0.0.60 test.zhangweibin.com" /etc/hosts
测试:浏览器访问:http://test.zhangweibin.com
是否会出来
该文件内容为测试php成功连接
5、安装mysql(5.7版)
#配置Mysql扩展源
rpm -ivh [http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm](https://links.jianshu.com/go?to=http%3A%2F%2Frepo.mysql.com%2Fyum%2Fmysql-5.7-community%2Fel%2F7%2Fx86_64%2Fmysql57-community-release-el7-10.noarch.rpm)
#yum安装
yum install mysql-community-server -y
systemctl start mysqld
systemctl enable mysqld
#使用Mysq初始密码登录数据库
cat /var/log/mysqld.log |grep root@localhost:
cat /var/log/mysqld.log |grep root@localhost: |awk -F " " '{print $NF}'
#修改MySQL密码简单的值
mysql -uroot -p
#密码输入:复制上一条的结果
set global validate_password_policy=0; #取消密码限制
set global validate_password_length=1; #修改密码最小值
set password for root@localhost = password('123456'); #修改密码为123456
6、安装workpress
mkdir -p /server/tools
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
tar xf wordpress-4.9.4-zh_CN.tar.gz -C /code/test
chown -R www.www /code/test/
mysql -uroot -p123456
create database wordpress;
exit
#在站点目录下把workpress数据库放在,注意第5行和第9行的路径
[root@chenhuichao /etc/nginx/conf.d]# cat -n /etc/nginx/conf.d/blog.zhangweibin.conf
1 server {
2 listen 80;
3 server_name blog.zhangweibin.com;
4 location / {
5 root /code/test/wordpress;
6 index index.php index.html;
7 }
8 location ~\.php$ {
9 root /code/test/wordpress;
10 fastcgi_pass 127.0.0.1:9000;
11 fastcgi_index index.php;
12 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
13 include fastcgi_params;
14 }
15 }
16