How to build a wordpress blog

发布于 2019-07-10  1034 次阅读


一、安装nginx, mysql, php

阿里云主机ubuntu 16.04 白板,要搭wordpress需要先装这三个东西,nginx 之前用django也用过,只用修改一下配置(之后再说);mysql 装5.6版本,php装7.3版本.

mysql安装:

# apt-get install software-properties-common
# add-apt-repository 'deb http://mirrors.cloud.aliyuncs.com/ubuntu trusty Release '
# apt-get update
# apt install mysql-server-5.6
# apt install mysql-client-5.6
# service mysql restart

mysql安装过程中会让你填密码(用户名root),这里我填了root,如果不填就是默认密码(记得是一长串随机字符来着,要翻配置找很麻烦)。

之后登入,创建一个DATABASE给wordpress用:

# mysql -u root -p
> create DATABASE wordpress;
> show databases;
+--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | mysql              |
 | performance_schema |
 | wordpress          |
 +--------------------+
 4 rows in set (0.01 sec)
> grant all privileges on databasename.* to "wordpressusername"@"hostname"  
-> IDENTIFIED BY "password";
> flush privileges;
> ^D

之后用创建的账号登录mysql成功,表示能用。

php安装:

参考网址:https://www.mf8.biz/ubuntu-debian-install-php7-3/

具体内容如下:

# apt -y install software-properties-common apt-transport-https lsb-release ca-certificates

# add-apt-repository ppa:ondrej/php  

# apt update

# apt install php7.3-fpm php7.3-mysql php7.3-curl php7.3-gd php7.3-mbstring php7.3-xml php7.3-xmlrpc php7.3-zip php7.3-opcache -y 

# sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/7.3/fpm/php.ini 

# systemctl restart php7.3-fpm

查看php是否安装成功:

# php -v
 PHP 7.3.7-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jul 10 2019 06:54:26) ( NTS )
 Copyright (c) 1997-2018 The PHP Group
 Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
     with Zend OPcache v7.3.7-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

二、解压、修改wordpress配置

参考网址:https://codex.wordpress.org/zh-cn:%E5%AE%89%E8%A3%85_WordPress#.E6.AD.A5.E9.AA.A43:.E8.AE.BE.E7.BD.AEwp-config.php

从官网上下载一个wordpress包,解压后得到一个wordpress文件夹,里面有一个wp-config-sample.php文件,打开进行修改,详细可以参考官方文档。

我的配置如下(要修改的地方):

define( 'DB_NAME', 'wordpress');
define( 'DB_USER', 'username');
define( 'DB_PASSWORD', 'userpassword');
define( 'DB_HOST', 'localhost');

define( 'AUTH_KEY', '...');
...

AUTH_KEY等去一个网站(https://api.wordpress.org/secret-key/1.1/salt)生成后填入就行了。

最后加上一堆东西(把原来的require_once(xxx)删掉用下面这个替代),可以保证加插件、换主题不会出bug:

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);
 require_once( ABSPATH . 'wp-settings.php' );

修改后把整个文件夹(wordpress)mv到/var/www/html/下,这是之后nginx配置文件里设定的地址。

也把权限改一下:

chmod 777 wp-content
cd wp-content
chmod 777 plugins
chmod 777 themes
chmod 777 upgrade
chmod 777 uploads

(反正就是wp-content下所有文件夹都改权限为可读可写可执行)

/var/www/html/wordpress

三、修改nginx配置

参考网址:https://www.centos.bz/2018/07/%E4%B8%80%E6%AC%A1ubuntunginx%E6%90%AD%E5%BB%BAwordpress%E7%9A%84%E7%BB%8F%E5%8E%86/

nginx配置文件为/etc/nginx/sites-available/default,直接修改(注意备份).

我的配置: (直接复制可能有点问题,因为初次使用wp一些地方还不会用,框里面是逐行拷贝,每一行左侧是5个空格而不是\t,但实际上应该用\t?)

server {
     listen 80 default_server;
     listen [::]:80 default_server ipv6only=on;
     listen 443 ssl; # ssl                   on;
     ssl_certificate         1838526_yyqtest.fun.pem;               
     ssl_certificate_key     1838526_yyqtest.fun.key;
     ssl_session_timeout     5m;
     ssl_protocols   SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2; 
     ssl_ciphers     HIGH:!aNULL:!MD5;
     ssl_prefer_server_ciphers on;
     root /var/www/html/wordpress;
     index index.html index.htm index.php;
     server_name localhost;
     charset utf-8;
     fastcgi_connect_timeout 300;
     fastcgi_send_timeout 300;
     fastcgi_read_timeout 300;
     client_max_body_size 75M;
     location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
     }
     location / {
          try_files $uri $uri/ /index.php?$args;
     }
}

1838526_yyqtest.fun.pem/key是当初申请域名和SSL证书时的东西,把它放在/etc/nginx就好.

之后重启nginx服务(# service nginx restart),再访问域名(yyqtest.fun),应该会直接进入wp的建站界面,按它的要求一步步做就行了。


WRYYYYYYYYY!