1 搭建LNMP环境
Linux:Linux 操作系统,本文以 CentOS 8.4 为例。
Nginx:Web 服务器,本文以 Nginx 1.18 为例。
mariadb:数据库,本文以 mariadb 10.4 为例。
PHP:脚本语言,本文以 PHP 7.4 为例。
1.1 Mariadb
1.1.1 Mariadb安装
添加源
vim /etc/yum.repos.d/MariaDB.repo ##新建配置文件
##将一下文件复制进去
# MariaDB 10.4 CentOS repository list - created 2020-06-01 04:02 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos8-amd64
module_hotfixes=1
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
保存退出后执行dnf install
sudo dnf install MariaDB-server
sudo systemctl start mariadb
1.1.2 Mariadb配置
执行以下命令,进入 MariaDB。
mysql
执行以下命令,创建 MariaDB 数据库。例如 “wordpress”。
CREATE DATABASE wordpress;
执行以下命令,创建一个新用户。例如 “user”,登录密码为 123456。
CREATE USER 'user'@'localhost' IDENTIFIED BY '123456';
执行以下命令,赋予用户对 “wordpress” 数据库的全部权限。
GRANT ALL PRIVILEGES ON wordpress.* TO 'user'@'localhost' IDENTIFIED BY '123456';
可以不用设置root账户登录密码
然后更新配置退出。
FLUSH PRIVILEGES;
\q
1.2 Nginx
1.2.1 nginx安装
dnf -y install http://nginx.org/packages/centos/8/x86_64/RPMS/nginx-1.18.0-1.el8.ngx.x86_64.rpm
可以去http://nginx.org/packages/centos/8/x86_64/RPMS/?spm=a2c4g.11186623.2.31.557423bfYPMd6u获取最新的包名并安装
查看版本
nginx -v
1.2.2 nginx配置
修改配置
cd /etc/nginx/conf.d
cp default.conf default.conf.bak
执行以下命令,打开 default.conf 文件。
vim default.conf
修改配置为下
server {
listen 80;
root /usr/share/nginx/html;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#
location / {
index index.php index.html index.htm;
}
#error_page 404 /404.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
注意fastcgi_pass和/etc/php-fpm.d/www.conf一致:
fastcgi_pass取unix:/run/php-fpm/www.sock;
/etc/nginx/conf.d的php-fpm.conf的server unix:/run/php-fpm/www.sock;
/etc/php-fpm.d/www.conf的listen = /run/php-fpm/www.sock
ubuntu需要添加nginx账户(不需登录的)
sudo useradd -s /sbin/nologin -M nginx
1.2.3 nginx其他配置
SSL配置
按照腾讯云SSL配置文档进行,下载证书并配置,具体配置可以看下方的配置详情。
文件上传2M限制解除
修改/etc/nginx/nginx.conf中头的大小限制。
client_max_body_size 100m;
另外还要修改/etc/php.ini文件,见后文。
修改文章固定链接后,打不开文章的问题解决
nginx配置中添加
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;//这行是为了防止打开后台、插件页等打不开的。
如果要使用文章名作为链接地址,应该安装pinyin slug的插件,自动转化为拼音链接。
nginx最终配置
最后我的配置default.conf配置如下,还配置了http转https、rewrite /wordpress / permanent;等,修改了server_name的配置。
server {
listen 443 ssl;
server_name *.sidney.wiki;
ssl_certificate sidney.wiki_bundle.crt;
ssl_certificate_key sidney.wiki.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE;
#ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
rewrite /wordpress / permanent;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
root /usr/share/nginx/html/wordpress;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location / {
index index.php index.html index.htm;
}
#error_page 404 /404.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}
}
server {
listen 80;
server_name *.sidney.wiki;
return 301 https://$host$request_uri;
}
1.3 PHP的安装和配置
1.3.1 PHP的安装
dnf -y install epel-release
dnf update epel-release
dnf clean all
dnf makecache
dnf module enable php:7.4
dnf install php php-curl php-dom php-exif php-fileinfo php-fpm php-gd php-hash php-json php-mbstring php-mysqli php-openssl php-pcre php-xml libsodium
php -v
启动
systemctl start php-fpm
systemctl enable php-fpm
验证
echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php
访问
http://云服务器实例的公网 IP/index.php
1.3.2 PHP的配置
修改文件为nginx的user和group
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
vi /etc/php-fpm.d/www.conf
ubuntu是/etc/php/8.3/fpm/pool.d/www.conf
搜索user以及grooup,把apache改为nginx
1.3.3 PHP有关特殊问题
照片裁剪错误解决
修改/etc/php.ini文件,最后的extension,然后重启php
[gd]
; Tell the jpeg decode to ignore warnings and try to create
; a gd image. The warning will then be displayed as notices
; disabled by default
; http://php.net/gd.jpeg-ignore-warning
;gd.jpeg_ignore_warning = 1
; extension = /usr/lib64/php/modules/gd.so
文件上传2M限制解除
修改/etc/nginx/nginx.conf中头的大小限制。
client_max_body_size 100m;
打开后/etc/php.ini文件,找到并设置以下选项的值:
upload_max_filesize = 100M
post_max_size = 105M
memory_limit = 128M
max_execution_time = 30
max_input_time = 60
2 wordpress搭建
2.1 下载wordpress
rm -rf /usr/share/nginx/html/index.php
cd /usr/share/nginx/html
wget https://cn.wordpress.org/wordpress-5.0.4-zh_CN.tar.gz
#可以去官网找最新版
tar zxvf wordpress-5.0.4-zh_CN.tar.gz
2.2 修改wordpress配置文件
cd /usr/share/nginx/html/wordpress
cp wp-config-sample.php wp-config.php
vim wp-config.php
配置文件修改为:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'user');
/** MySQL database password */
define('DB_PASSWORD', '123456');
/** MySQL hostname */
define('DB_HOST', 'localhost');
为了支持更新插件和素材,需要在此文件中再添加一下配置:
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
define('WP_TEMP_DIR', ABSPATH.'wp-content/tmp');
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);
define('ALLOW_UNFILTERED_UPLOADS', true);
在/usr/share/nginx/html/wordpress/wp-content/中创建一个tmp目录,权限nginx:nginx,
并且修改wordpress文件夹为nginx可以访问的权限
chown -R nginx:nginx /usr/share/nginx/html/wordpress
3 wordpress使用
url/wp-login.php去登录
注意后台设置url要设置成真实的url。
4 参考文章
腾讯云wordpress配置
手动搭建LNMP环境
mariadb安装
nginx ssl证书部署
ubuntu:
https://www.koogua.com/article/432
真棒!