lnmp工作原理:
首先nginx不能处理动态的请求,那么当用户发起动态请求的时候,nginx他是如何处理的?
当用户发起http请求,请求会被nginx处理,如果是静态资源请求,nginx就会直接返回,如果是动态请求nginx则会通过fastcgi协议转交给后端的php处理
配置
首先统一用户,php和nginx的用户要统一,要不然就像rsync+lsync一样,用户不一致,写入就会有问题,不会出现权限的问题
[root@test3 ~]# groupadd -g777 www
[root@test3 ~]# useradd www -s /sbin/nologin -M -u777 -g777
1.安装nginx
[root@test3 ~]# yum install nginx -y
[root@test3 ~]# sed -i '5s#nginx#www#' /etc/nginx/nginx.conf
[root@test3 ~]# systemctl restart nginx
[root@test3 ~]# netstat -tunlp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 69892/nginx: master
[root@test3 ~]# ps -ef|grep nginx
root 51645 46443 0 Aug16 pts/2 00:00:00 vim /etc/nginx/nginx.conf
root 69892 1 0 03:47 ? 00:00:00 nginx: master process /usr/sbin/nginx
www 69893 69892 0 03:47 ? 00:00:00 nginx: worker process
www 69894 69892 0 03:47 ? 00:00:00 nginx: worker process
root 69918 50057 0 03:48 pts/0 00:00:00 grep --color=auto nginx
此时子进程的用户就变成www了
安装php
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
# 安装php7版本的依赖
yum install -y 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 php71w-json php71w-pecl-apcu php71w-pecl-apcu-devel
修改配置文件
[root@test3 ~]# grep -E '^(user|group)' /etc/php-fpm.d/www.conf
user = www
group = www
[root@test3 ~]# systemctl restart php-fpm
[root@test3 ~]# netstat -tunlp|grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 77661/php-fpm: mast
1,安装数据库
[root@test3 ~]# yum install mariadb-server mariadb -y
[root@test3 ~]# systemctl start mariadb
[root@test3 ~]# mysqladmin password '123456'
配置php连接mysql服务
[root@test3 nginx]# cat conf.d/test.conf
server {
listen 80;
server_name www.xp.com;
location / {
root /code/;
index index.php index.html;
}
location ~ \.php$ {
root /code/;
# 会吧请求转发给后端的php-fpm进行加工
fastcgi_pass 127.0.0.1:9000;
# 默认的首页文件名字是index.php
fastcgi_index index.php;
# 转发的参数,
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重启nginx
[root@test3 conf.d]# systemctl restart nginx
确保php-fpm进程在
[root@test3 conf.d]# ps -ef|grep php-fpm
root 77661 1 0 06:24 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
www 77662 77661 0 06:24 ? 00:00:00 php-fpm: pool www
www 77663 77661 0 06:24 ? 00:00:00 php-fpm: pool www
www 77664 77661 0 06:24 ? 00:00:00 php-fpm: pool www
www 77665 77661 0 06:24 ? 00:00:00 php-fpm: pool www
www 77666 77661 0 06:24 ? 00:00:00 php-fpm: pool www
root 83587 46443 0 08:17 pts/2 00:00:00 grep --color=auto php-fpm
设置权限
[root@test3 conf.d]# mkdir -p /code
[root@test3 conf.d]# cd /code
[root@test3 code]#
[root@test3 code]# chown -R www.www /code
第二步,在/code下写一个查看本身php信息的代码
创建一个测试数据,创建phpinfo页面
cat > /code/index.php <<EOF
<?php
phpinfo();
?>
EOF
访问测试
测试mysql连接是否正常
[root@test3 code]# cat mysql.php
<?php
$server="127.0.0.1";
$mysql_user="root";
$mysql_pwd="123456";
// 创建数据库连接
$conn=mysqli_connect($server,$mysql_user,$mysql_pwd);
// 检测连通性
if($conn){
echo "mysql successful by yuchaoit.cn \n";
}else {
die( "Connection failed: " . mysqli_connect_error());
}
?>
测试访问
部署wordPress
1.安装基础的框架
nginx+mysql+php
2.更改配置文件
[root@test3 code]# cat /etc/nginx/conf.d/wordpress.conf
server {
listen 80;
server_name www.wordpress.com;
root /code/wordpress/;
location / {
index index.php index.html;
}
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;
}
}
[root@test3 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@test3 code]# systemctl restart nginx
[root@test3 code]#
创建存放wordpress的代码目录
[root@test3 code]# mkdir /code/wordpress/
[root@test3 code]# chown www.www /code/wordpress/
下载wordpress代码
https://cn.wordpress.org/download/
[root@test3 wordpress]# pwd
/code/wordpress
[root@test3 wordpress]# wget https://cn.wordpress.org/wordpress-5.0.3-zh_CN.zip
--2024-08-17 10:11:30-- https://cn.wordpress.org/wordpress-5.0.3-zh_CN.zip
Resolving cn.wordpress.org (cn.wordpress.org)... 198.143.164.252
Connecting to cn.wordpress.org (cn.wordpress.org)|198.143.164.252|:443... connected.
HTTP request sent, awaiting response... [root@test3 wordpress]# unzip wordpress-5.0.3-zh_CN.zip
Archive: wordpress-5.0.3-zh_CN.zip
creating: wordpress/
inflating: wordpress/wp-login.php
inflating: wordpress/wp-cron.php
inflating: wordpress/xmlrpc.php
inflating: wordpress/wp-load.php
[root@test3 wordpress]# cd wordpress
[root@test3 wordpress]# ls
index.php wp-activate.php wp-comments-post.php wp-cron.php wp-load.php wp-settings.php xmlrpc.php
license.txt wp-admin wp-config-sample.php wp-includes wp-login.php wp-signup.php
readme.html wp-blog-header.php wp-content wp-links-opml.php wp-mail.php wp-trackback.php
[root@test3 wordpress]# pwd
/code/wordpress/wordpress
[root@test3 wordpress]# ls
index.php wp-activate.php wp-comments-post.php wp-cron.php wp-load.php wp-settings.php xmlrpc.php
license.txt wp-admin wp-config-sample.php wp-includes wp-login.php wp-signup.php
readme.html wp-blog-header.php wp-content wp-links-opml.php wp-mail.php wp-trackback.php
[root@test3 wordpress]# cd ..
[root@test3 wordpress]# ls
wordpress wordpress-5.0.3-zh_CN.zip
[root@test3 wordpress]# mv wordpress/* .
[root@test3 wordpress]# ls
index.php wordpress-5.0.3-zh_CN.zip wp-comments-post.php wp-includes wp-mail.php xmlrpc.php
license.txt wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php
readme.html wp-admin wp-content wp-load.php wp-signup.php
wordpress wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php
此时博客就搭建完成了
点击现在就开始,创建对应的文件
[root@test3 wordpress]# mysql -uroot -p'123456'
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.68-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 wordpress;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
表前缀,就是到时候他创建表时以什么什么开头的,我们让他以wp开头的就可以了,
点击提交,点击开始安装
按要求填写
注意时间同步
如果还不行,看nginx日志
部署知乎的wecenter,论坛
[root@test3 wecenter]# cat /etc/nginx/conf.d/wecenter.conf
server {
listen 80;
server_name www.wecenter.com;
root /code/wecenter/;
location / {
index index.php index.html;
}
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;
}
}
[root@test3 conf.d]# systemctl restart nginx
[root@test3 conf.d]# mkdir /code/wecenter/
[root@test3 conf.d]# chown www.www /code/w
wecenter/ wordpress/
[root@test3 conf.d]# chown www.www /code/wecenter/
上传安装包,解压
[root@test3 wecenter]# pwd
/code/wecenter
[root@test3 wecenter]# ls
app composer.json install models README.md system vendor WeCenter V3.6.2.zip
cache composer.lock language nginx.htaccess robots.txt tmp version.php
changelog.txt index.php license.txt plugins static uploads views
根据打叉的来进行修改
[root@test3 wecenter]# chown www.www -R /code/wecenter/
此时就可以了
创建库,用于写入
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> create database wecenter;
Query OK, 1 row affected (0.00 sec)
拆分lnmp
1.首先把test3的mysql给分离出去,缓解一台服务器的压力
mysqldump
2.把test3的/code给rsync到另一台机器,另一台机器通过nfs来挂载到本机的/code下。缓解服务器存储的压力
3.另起一台新的机器做负载均衡,
另一台机器需要安装nginx,php-fpm
在创建好对应的用户
rsync -az --delete test3:/etc/nginx/ /etc/nginx/
scp test3:/etc/php-fpm.d/www.conf /etc/php-fpm.d/
mkdir /code
mount -t nfs test1:/code /code
做hosts解析
重启nginx即可