美文网首页程序员
基于LNMP搭建wordpress博客系统

基于LNMP搭建wordpress博客系统

作者: 南南宫问天 | 来源:发表于2020-05-10 23:54 被阅读0次

LNMP是一个基于linux搭建的动态网页架构服务,主要组成服务部件有NGINX+MYSQL+PHP

Nginx: 处理用户的静态请求
PHP: 处理动态的页面请求 负责和数据库建立关系
Mysql: 存储用户的字符串数据信息


image.png

nginx(fastcgi_pass) ---FastCGI-->(php-fpm-- wrapper) php(php解析器) + mysql(读取或写入)

nginx的安装和配置

1.关闭防火墙和selinux

[root@web01 ~]# systemctl stop firewalld ; systemctl disable firewalld
[root@web01 ~]# setenforce 0 ; sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/sysconfig/selinux

2.添加nginx官方源

[root@web01 ~]# cat /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
module_hotfixes=true

3.安装nginx

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

4.启动nginx

[root@web01 ~]# systemctl restart nginx ; systemctl enable nginx
[root@web01 ~]# netstat -lntup | grep ":80" ##查看端口启动成功
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2419/nginx: master 

5.配置nginx

[root@web01 ~]# useradd czq -s /sbin/nologin ##创建一个nginx管理用户,并设置不能登录系统
[root@web01 ~]# vim /etc/nginx/nginx.conf ##编辑nginx主配置文件
user  czq;  ##将user改成czq
worker_processes  2;  ##work线程可以设置两个(推荐cpu的乘2的work线程数)
[root@web01 ~]# cat /etc/nginx/conf.d/blog.conf  ##创建一个nginx博客主页配置文件
server {
  listen 80; ##定义监听端口
  server_name czq.blog.com; ##定义访问域名
  location / {  ##定义相关站点目录
  root /html/blog/;  
  index index.html;
}
}
[root@web01 ~]# mkdir /html/blog -p ##创建站点目录
[root@web01 ~]# echo "hello nginx" > /html/blog/index.html ##先定义一个主页文件测试
[root@web01 ~]# systemctl reload nginx ##平滑重启nginx

6.在客户端主机添加hosts解析

172.16.210.51 blog.czq.com

7.访问测试

image.png

访问成功 nginx搭建完成

MYSQL安装

1.安装mysql

这里我们用mariadb代替mysql

[root@web01 ~]# yum remove -y mariadb-libs.x86_64  ##先移除系统内核自带mariadb-libs,不然以后安装mariadb会包冲突
[root@web01 ~]# yum install -y mariadb-server mariadb

2.启动mariadb服务并加入开机自启

[root@web01 ~]# systemctl start mariadb ; systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

初始化数据库

[root@web01 ~]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

PHP部署

1.添加相关源,并安装PHP和一些相关php工具

root@web01 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
获取https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
警告:/var/tmp/rpm-tmp.Ita3O5: 头V3 RSA/SHA256 Signature, 密钥 ID 352c64e5: NOKEY
准备中...                                                            ################################# [100%]
正在升级/安装...
   1:epel-release-7-12                                               ################################# [100%]
[root@web01 ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
获取https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
警告:/var/tmp/rpm-tmp.3V3ZWL: 头V4 RSA/SHA1 Signature, 密钥 ID 62e74ca5: NOKEY
准备中...                                                            ################################# [100%]
正在升级/安装...
   1:webtatic-release-7-3                                            ################################# [100%]
[root@web01 ~]# yum install -y 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配置文件

[root@web01 ~]# vim /etc/php-fpm.d/www.conf 
user = czq  ##把user改成nginx进程的管理用户
group = czq ##把组也改成改成nginx进程的管理用户

3.启动php并加入开机启动

[root@web01 ~]# systemctl start php-fpm.service ; systemctl enable php-fpm.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

让nginx和php建立关系

1.编写php配置文件

[root@web01 ~]# cat /etc/nginx/conf.d/blog.conf 
server {
  listen 80;
  server_name czq.blog.com;
  location / {
  root /html/blog;
  index  index.php index.html; ##添加index.php,使nginx能找到php格式的主页文件
}
location ~ \.php$ {
    root /html/blog; ##定义站点目录
    fastcgi_index index.php; ##定义nginx读取php的主页文件
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ##定义nginx读取php主页文件的url和uri
    fastcgi_pass  127.0.0.1:9000; ##定义nginx读取php接口地址
    include fastcgi_params;  #变量配置文件
       }
}

2.重启nginx

[root@web01 ~]# systemctl restart nginx

3.编写动态网页测试

[root@web01 ~]# cat /html/blog/test_php.php 
<?php
phpinfo(); ##调用php的函数
?>

4.访问测试

image.png

成功解析php文件,做到了nginx关联php

搭建wordpress博客

wordpress压缩包地址 https://wordpress.org/download/

1.提前下载好包并上传到服务器

[root@web01 ~]# ll
总用量 12728
-rw-r--r--  1 root root 12234700 5月  10 23:17 wordpress-5.4.1.tar.gz

2.解压wordpress包到站点目录

[root@web01 ~]# tar -zxf wordpress-5.4.1.tar.gz ##解压包
root@web01 ~]# mv wordpress/* /html/blog/ ##移动包里的文件到网站目录

3.修改站点目录权限

[root@web01 ~]# cd /html/
[root@web01 html]# chown -R czq.czq blog/ ##递归修改blog文件夹和里面的文件的属主和属组为nginx管理用户

创建wordpress数据库

[root@web01 ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
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 wordpress; ##创建wordpress数据库
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on wordpress.* to 'wordpress'@'localhost' identified by 'redhat'; ##创建wordpress用户和设置密码还有设置对其wordpress数据库的权限
Query OK, 0 rows affected (0.00 sec)

5.网页初始化

浏览器输入blog.czq.com/index.php

image.png 点击Lets go ! image.png 设置好相关数据库和用户和密码点击Submit image.png
点击Run teh installation开始运行wordpress安装准备 image.png
设置好相关用户名和密码点击Install WordPres 开始安装wordpress image.png
点击Log In
image.png
输入好用户名和密码就可以登录了

6.发布博文

image.png 点击Ports 也就是博文 image.png
点击Add New添加新博文 image.png
点击Publish.. 发布博文 image.png

把这个地址发给好友,就可以让其他人查看博文了(前提底层服务器必须有公网地址和绑定了解析域名)

image.png

访问博文

相关文章

网友评论

    本文标题:基于LNMP搭建wordpress博客系统

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