搭建WordPress(LEMP环境)

LEMP环境的安装

1.LEMP环境指 Linux, Nginx, MariaDB或者MySQL 以及 PHP,本例中系统Debian 10

2.更新软件包

sudo apt update && sudo apt upgrade

3.安装Nginx,MariaDB,php7.3

sudo apt install -y nginx-extras mariadb-server mariadb-client php7.3 php7.3-fpm php7.3-mysql php-common php7.3-cli php7.3-common php7.3-json php7.3-opcache php7.3-readline

4. MariaDB的初始化配置

sudo mysql_secure_installation

根据提示设定MariaDB的root密码

5.Wordpress的目录安装

sudo apt install -y curl wget unzip && curl -O https://wordpress.org/latest.zip && unzip latest.zip -d /var/www/

6.为wordpress创建一个MariaDB数据库

sudo mysql -u root

create database wordpress;
grant all privileges on wordpress.* to wordpressadmin@localhost identified by 'your-password';
flush privileges;
exit;

7.设置wordpress

cp /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php
sudo sed -i 's/database_name_here/wordpress/g' /var/www/wordpress/wp-config.php
sudo sed -i 's/username_here/wordpressadmin/g' /var/www/wordpress/wp-config.php
sudo sed -i 's/password_here/your-password/g' /var/www/wordpress/wp-config.php

为确保nginx有权限写入wordpress根目录,需修改wordpress根目录所有者

sudo chown www-data:www-data /var/www/wordpress/ -R

8.为Nginx服务器签发TLS证书

9.配置Nginx并启动

  • 创建nginx server配置文件

sudo touch /etc/nginx/conf.d/yourdomain.conf

  • 将以下内容粘贴进/etc/nginx/conf.d/yourdomain.conf
#Upstream to abstract backend connection(s) for php
upstream php {
         server unix:/run/php/php7.3-fpm.sock;
        ## server 127.0.0.1:9000;
 }
server {
     listen 80 default_server;
     listen [::]:80 default_server;
return 301 https://$host$request_uri;
 } 
 server {
         ## Your website name goes here.
         server_name yourdomain.com;
     # listens both on IPv4 and IPv6 on 443 and enables HTTPS and HTTP/2 support.
     # HTTP/2 is available in nginx 1.9.5 and above.
     listen 443 ssl http2;
     listen [::]:443 ssl http2;
     # indicate locations of SSL key files.
     ssl_certificate /path/yourdomain.com.crt;
     ssl_certificate_key /path/yourdomain.com.key;
        ## Your only path reference.
    root /var/www/wordpress;
    ## This should be in your http block and if it is, it's not needed here.
    index index.php;
    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }
    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }
    location / {
            # This is cool because no php is touched for static content.
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            include fastcgi.conf;
            fastcgi_intercept_errors on;
            fastcgi_pass php;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }
	}

启动各项服务

sudo nginx -t && sudo systemctl start nginx && sudo systemctl enable nginx

sudo systemctl start mariadb && sudo systemctl enable mariadb

sudo systemctl start php7.3-fpm && sudo systemctl enable php7.3-fpm

浏览器登陆 https://yourdomain.com/wp-admin/install.php,完成设置