Ubuntu上安装WordPress

1 安装PHP

要在Ubuntu系统中添加PHP 8.1的官方软件源,你可以按照以下步骤操作:
1.更新系统软件包列表
执行以下命令以确保你的系统软件包列表是最新的:

sudo apt update

2.安装必要的依赖包
安装添加PPA(Personal Package Archive)所需的依赖包:

sudo apt-get install ca-certificates apt-transport-https software-properties-common wget curl lsb-release -y

3.添加Ondřej Surý的PPA存储库
Ondřej Surý是负责Debian和Ubuntu中PHP软件包的开发者,他的PPA提供了最新版本的PHP。执行以下命令来添加他的PPA:

sudo add-apt-repository ppa:ondrej/php

4.再次更新软件包列表
添加PPA后,再次更新软件包列表以包含新添加的源:

sudo apt update

5.安装PHP 8.1及其相关模块
现在你可以安装PHP 8.1及其所需的模块了。例如,如果你需要安装PHP 8.1和一些常用的扩展,可以使用以下命令:

sudo apt install php8.1 php8.1-cli php8.1-fpm php8.1-mysql php8.1-opcache php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl -y

6.验证安装
安装完成后,你可以通过运行以下命令来验证PHP版本,确保安装的是8.1版本:

php -v

以上步骤将帮助你在Ubuntu系统中添加PHP 8.1的官方软件源并进行安装。这些步骤基于PHP官方推荐的Ondřej Surý的PPA,这是一个广泛认可和使用的源,用于在Ubuntu系统中安装最新版本的PHP 。

2 安装mysql

1.安装

sudo apt install mysql-server

2.创建mysql数据库root用户

# 登录mysql
sudo mysql

# 创建root账户
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';

# 退出
EXIT;

# 用新的root账号重新登录
mysql -u root -p
# 后面会要输入密码,就是前面创建root账号时设置的密码(new_password)

3.运行安全安装脚本以设置 MySQL 的安全选项

#退出mysql数据库终端
EXIT;

# 设置安全选项
sudo mysql_secure_installation

按照提示操作,设置 root 密码并删除匿名用户、不允许远程 root 登录等。

4.创建wordpress的数据库

# 用root账号重新登录
mysql -u root -p

CREATE DATABASE wordpress;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;

#退出mysql数据库终端
EXIT;

3 安装wordpress

# 下载wordpress包到家目录下
cd ~/
wget https://wordpress.org/latest.tar.gz

# 解压
tar -vxf latest.tar.gz

# 挪到nginx服务器读取网页的目录下
sudo mv wordpress /var/www/html/

# 删除下载的包(可选)
rm latest.tar.gz

#给/var/www/html/目录设置权限
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress

4 Nginx设置

打开 /etc/nginx 下的 nginx.conf 文件,添加如下配置:

# bettergogo-root
server {
  listen 443 ssl http2;
  keepalive_timeout 70;
  server_name xxx.com; # 你的域名

  root /var/www/html/wordpress; # 你的前端应用的根目录
  index index.php index.html index.htm default.php default.htm default.html;

  # 爬虫请求头
  add_header X-Robots-Tag "index, follow";

  location / {
      try_files $uri $uri/ /index.html;
  }

  location ~ \.php$ {
          include snippets/fastcgi-php.conf;
               fastcgi_pass unix:/run/php/php8.1-fpm.sock;
          }
  location ~ /\.ht {
               deny all;
          }

  # ssl证书
  ssl_certificate /etc/letsencrypt/live/bettergogo.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/bettergogo.com/privkey.pem; # managed by Certbot
}

免费获取SSL证书教程参见:从Let’s Encrypt获取SSL证书(HTTPS证书)

5 登录 WordPress 后台

访问以上域名,即可登录后台,然后进行设置用户名和密码。

6 问题处理

6.1 安装PHP报错

~# sudo apt-get install php8.1 php8.1-cli php8.1-fpm php8.1-mysql php8.1-opcache php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package php8.1
E: Couldn't find any package by glob 'php8.1'
E: Couldn't find any package by regex 'php8.1'
E: Unable to locate package php8.1-cli
E: Couldn't find any package by glob 'php8.1-cli'
E: Couldn't find any package by regex 'php8.1-cli'
E: Unable to locate package php8.1-fpm
E: Couldn't find any package by glob 'php8.1-fpm'
E: Couldn't find any package by regex 'php8.1-fpm'
E: Unable to locate package php8.1-mysql
E: Couldn't find any package by glob 'php8.1-mysql'
E: Couldn't find any package by regex 'php8.1-mysql'
E: Unable to locate package php8.1-opcache
E: Couldn't find any package by glob 'php8.1-opcache'
E: Couldn't find any package by regex 'php8.1-opcache'
E: Unable to locate package php8.1-mbstring
E: Couldn't find any package by glob 'php8.1-mbstring'
E: Couldn't find any package by regex 'php8.1-mbstring'
E: Unable to locate package php8.1-xml
E: Couldn't find any package by glob 'php8.1-xml'
E: Couldn't find any package by regex 'php8.1-xml'
E: Unable to locate package php8.1-gd
E: Couldn't find any package by glob 'php8.1-gd'
E: Couldn't find any package by regex 'php8.1-gd'
E: Unable to locate package php8.1-curl
E: Couldn't find any package by glob 'php8.1-curl'
E: Couldn't find any package by regex 'php8.1-curl'

直接通过 sudo apt-get install php8.1 php8.1-cli php8.1-fpm php8.1-mysql php8.1-opcache php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl -y 安装php 可能会报以上错误,按照本教程中的方法(基于PHP官方推荐的Ondřej Surý的PPA安装)处理即可。

7 参考资料

  1. ubuntu+nginx搭建wordpress的建站教程

Comments

No comments yet. Why don’t you start the discussion?

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注