背景
基于 Ubuntu22.04,安装 mysql php-fpm wordpress nginx 。
目前 2023年8月,各个默认版本:MySQL 8.0.33,PHP 8.1.27,Nginx 1.18,WordPress 5.9.8
流程
Mysql
- 安装
sudo apt update
sudo apt install mysql-server
systemctl status mysql
mysql -u root -p
- 配置用户
use mysql;
select host,user,authentication_string from user;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root_password';
# 创建wordpress用户
CREATE USER 'wordpressuser'@'%' IDENTIFIED WITH mysql_native_password BY 'wp_password';
# 创建wordpress数据库
CREATE DATABASE wordpress;
# 给用户授权
GRANT ALL ON wordpress.* TO 'wordpressuser'@'%';
# 刷新权限
FLUSH PRIVILEGES;
exit;
systemctl status mysql
查看mysql状态,如果出现Active: active (running)
,则说明安装成功。
PHP
- 安装
sudo apt install php php-fpm php-mysql php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip
sudo systemctl restart php8.1-fpm.service
sudo systemctl status php8.1-fpm.service
查看php-fpm状态,如果出现Active: active (running)
,则说明安装成功。
- 卸载php
# 删除php的相关包及配置
sudo apt-get autoremove php*
#删除关联
sudo find /etc -name "*php*" |xargs rm -rf
#清除dept列表
sudo apt purge `dpkg -l | grep php| awk '{print $2}' |tr "n" " "`
#检查是否卸载干净(无返回就是卸载完成)
dpkg -l | grep php
Wordpress
- 安装
cd /tmp
# 下载 wordpress 包
curl -LO https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
# 复制wp-config.php
cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
# 复制到站点目录
sudo cp -a /tmp/wordpress/. /var/www/html/wordpress
# 修改权限
sudo chown -R www-data:www-data /var/www/html/wordpress
- 修改 wordspress 的配置文件:
vim /var/www/html/wordpress/wp-config.php
define( 'DB_NAME', 'wordpress' );
/** Database username */
define( 'DB_USER', 'wordpressuser' );
/** Database password */
define( 'DB_PASSWORD', 'wp_password' );
/** Database hostname */
define( 'DB_HOST', 'localhost' );
Nginx
- 安装
sudo apt install nginx -y
# 删除默认配置文件
sudo rm -f /etc/nginx/sites-enabled/default
# 重启nginx
nginx -t
nginx -s reload
systemctl status nginx
查看nginx状态,如果出现Active: active (running)
,则说明安装成功。
- wordpress nginx 配置
vim /etc/nginx/sites-enabled/wordpress.conf
server {
listen 80;
server_name YOUR_DOMAIN.COM;
root /var/www/html/wordpress; # WordPress目录
index index.php index.html index.htm;
#图片缓存86400秒(1天)
location ~* .(gif|jpg|jpeg|png|webp)$ {
expires 86400;
}
#HTML/CSS/JS缓存2592000秒(30天)
location ~* .(htm|html|css|js)$ {
expires 2592000;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
#禁止访问.htaccess文件
location ~ /.ht {
deny all;
}
}
- 重启nginx
nginx -t
nginx -s reload
使用
访问nginx里配置的 YOUR_DOMAIN.COM,选择语言,配置站点信息,然后就会跳转到 wordpress 后台,大功告成。