LNMP架构部署wordpress个人博客

说明

  • LNMP经典网站环境,Linux系统,Nginx网站服务,MySQL数据库(Mariadb),PHP(运行环境)
  • Wordpress PHP代码

建议的搭建顺序

  • MySQL数据库(mariadb)
  • PHP环境 php7.x
  • Nginx直接安装即可

部署数据库

安装数据库

 apt install -y mariadb-server mariadb-client

启动数据库

 systemctl enable --now mariadb

检查

 #检查端口
 ss -lntup | grep mariadb
 
 #检查jincheng
 ps -ef | grep mariadb

数据库初始化(优化)

  #安装后进行即可,进行1次即可
 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
 haven't set the root password yet, you should just press enter here.
 
 Enter current password for root (enter for none):   #回车
 OK, successfully used password, moving on...
 
 Setting the root password or using the unix_socket ensures that nobody
 can log into the MariaDB root user without the proper authorisation.
 
 You already have your root account protected, so you can safely answer 'n'.
 
 Switch to unix_socket authentication [Y/n] n        #no  
  ... skipping.
 
 You already have your root account protected, so you can safely answer 'n'.
 
 Change the root password? [Y/n] Y                   #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                    #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              #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] Y     #Y
  - Dropping test database...
  ... Success!
  - Removing privileges on test database...
  ... Success!
 
 Reloading the privilege tables will ensure that all changes made so far
 will take effect immediately.
 
 Reload privilege tables now? [Y/n] Y              #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!

进入数据库,创建库,创建用户

 #-u 用户名
 #-p 密码
 
 #使用root登录 #密码为root密码
 mysql -uroot -p
 
 #出现以下提示为进入成功
 Enter password: 
 Welcome to the MariaDB monitor.  Commands end with ; or \g.
 Your MariaDB connection id is 39
 Server version: 10.6.18-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04
 
 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 tatabase wordpress; #创建一个叫wordpress的库
 
 #添加用户并检查
 grant all on wordpress.* to 'wordpress'@'localhost' identified by 'yangfan996'
 #上面解析:👆
 #给予所有all(所有)权限对于wordpress这个库下所有 wordpress用户登录方式为本地登录 用户密码:yangfan996
 
 #通过wordpress用户登录测试
 mysql -uwordpress -pyangfan996

一些删除操作:

1.删除数据库:drop database wordpress;

2.删除用户:drop user 'wordpress'@'localhost'; 刷新权限 flush privileges;

👆22行说明:grent all on wordpress.* to 'wordpress'@'localhost' identified by 'lidao996';

添加用户 设置权限

grent all on wordpress.* to 'wordpress'@'localhost' identified by 'Lidao996'

所有权限对于wordpress库所有表 to‘用户’@‘白名单(本地登录)’ 密码是Lidao996

PHP环境部署

 #安装php
 #1.生成本地apt缓存
 apt update
 
 #2.安装下面的软件包
 apt install -y  php8.1-bcmath php8.1-bz2 php8.1-cgi php8.1-cli php8.1-common php8.1-curl php8.1-fpm php8.1-gd php8.1-intl   \
 php8.1-mbstring php8.1-mysql php8.1-opcache php8.1-readline php8.1-soap \
 php8.1-xml php8.1-zip php8.1-apcu php8.1-redis php8.1-snmp
 
 #2.检查数量
 dpkg -l | grep php8.1|wc -l

修改配置文件,检查启动服务

 #1.查看软件包的配置文件
 dpkg -L php8.1-fpm
 
 /etc/php/8.1/fpm/php-fpm.conf #php-fpm主配置文件
 /etc/php/8.1/fpm/pool.d
 /etc/php/8.1/fpm/pool.d/www.conf #子配置文件.
 
 #2.修改子配置文件
 vim /etc/php/8.1/fpm/pool.d/www.conf
 #修改后查看👇
 egrep '^(user|group|listen)' /etc/php/8.1/fpm/pool.d/www.conf
 user = www-data
 group = www-data
 listen = 127.0.0.1:9000      #主要修改这里
 listen.owner = www-data
 listen.group = www-data
 
 #user和group 看后续nginx安装后的用户
 
 #3.检查配置文件
 php-fpm8.1 -t
 
 输出为以下👇
 [14-Sep-2024 10:21:34] NOTICE: PHP message: PHP Warning:  Cannot load module "http" because required module "raphf" is not loaded in Unknown on line 0
 [14-Sep-2024 10:21:34] NOTICE: configuration file /etc/php/8.1/fpm/php-fpm.conf test is successful
 
 用echo $?检查是否错误,没有则返回0
 #4.启动(重启)
 dpkg -L php8.1-fpm | grep service
 
 #启动php8.1-fpm
 systemctl enable  --now php8.1-fpm
 
 #查看输出是否有错误
 echo $?
 
 #重启php8.1-fpm
 systemctl restart php8.1-fpm

nginx

 #检查是否安装apache
 #关闭apache2服务
 apt autoremove apache2 apache2-bin
 
 #安装nginx
 apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
 
 下载nginx配置文件
 curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
   | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
 
 gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
 
 #上面命令执行后应该有如下输出:类似的👇
 pub   rsa2048 2011-08-19 [SC] [expires: 2027-05-24]
       573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
 uid                      nginx signing key <signing-key@nginx.com>
 
 echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
 http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
     | sudo tee /etc/apt/sources.list.d/nginx.list
 
 apt update #生成本地apt缓存
 
 apt install -y nginx  #下载nginx
 
 dpkg -l  | grep nginx  #查看nginx是否安装
 
 #3.修改nginx配置文件
 /etc/nginx/nginx.conf  #主配置文件
 /etc/nginx/conf.d 
 /etc/nginx/conf.d/default.conf  #子配置文件
 
 #3.1修改nginx用户为www-data\
 grep ^user /etc/nginx/nginx.conf
 user www-dada
 
 #4.修改配置文件
 cp /etc/nginx/conf.d/default.conf{,.bak} #备份子配置文件
 vim /etc/nginx/conf.d/default.conf #修改子配置文件
 cat /etc/nginx/conf.d/default.conf #修改后查看如下👇
 server {
     listen       80;
     server_name  localhost;
     root /app/code/blog/;
     location / {
         index  index.php;
     }
     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;
     }
 }
 
 #5.检查语法,启动/重启服务
 nginx -t #检查nginx文件配置参数
 echo $? #查看输出是否有错
 systemctl enable --now nginx #启动nginx服务(开机自启)
 #输出以下👇
 Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
 Executing: /lib/systemd/systemd-sysv-install enable nginx

部署代码(业务)

#1.创建代码目录
mkdir -p /app/code/blog

#2.下载代码
#下载网址:https://cn.wordpress.org/download/

#下载压缩包
wget https://cn.wordpress.org/latest-zh_CN.zip

#3.解压代码移动到网站目录
unzip -t latest-zh_CN.zip #检查安装包完整性 正确输出OK,错误可能输出bad CRC (inflater)
unzip latest-zh_CN.zip  #解压到当前目录
mv wordpress/* /app/code/blog/  #移动wordpress下所有到指定目录(防止因为多出来的wordpress目录而找不到wordpress下文件)

#4.设置所属用户
chown -R www-data.www-data /app/code/blog/

#5.待补充

放行网页访问(80)端口

5.浏览器搜索域名自动下载域名的文件

原因:可能由于nginx服务没有转发给php服务建议检查nginx子配置文件

posted @ 2025-03-04 17:08  zc918  阅读(118)  评论(0)    收藏  举报