Ubuntu16安装wordpress+nginx+mysql

安装环境:ubuntu16.04 LTS
最开始尝试用centos7去安装,centos7下的mysql已经换成了mariadb(mysql的一个分支),mariadb整了大概四个小时,没整过。无奈放弃。

安装软件

要搭建wordpress,我们需要安装nginxmysqlphpphp-fpmphp-mysqlphp-mbstring这些软件:

  • php-fpm:用于执行与Nginx的PHP文件的标准选择的工具
  • php-mysql:用于php与mysql的连接
  • php-mbstring:用于phpMyAdmin

在安装前,先执行apt-get update更新一下源,以保证安装顺利通过。

apt-get install nginx
apt-get install mysql-server
apt-get install php
apt-get install php-fpm
apt-get install php-mysql
apt-get install php-mbstring

注意,安装mysql时,会让你填写mysql root用户密码,记下来。

下载wordpress最新版:

wget https://wordpress.org/latest.tar.gz

比如我的站点名称是zaixianku.com,下文相同。解压到/var/www/zaixianku.com:

tar -C /var/www -zxvf latest.tar.gz
mv /var/www/wordpress /var/www/zaixianku.com

下载phpMyAdmin最新版

wget https://files.phpmyadmin.net/phpMyAdmin/4.7.2/phpMyAdmin-4.7.2-all-languages.zip

解压到站点目录下

unzip phpMyAdmin-4.7.2-all-languages.zip
mv phpMyAdmin-4.7.2-all-languages phpMyAdmin
mv phpMyAdmin  /var/www/zaixianku.com

配置

配置php-fpm

打开php-fpm的配置文件:

vi /etc/php/7.0/fpm/php.ini

查找cgi.fix_pathinfo参数并修改它看起来像这样:

cgi.fix_pathinfo=0

这是一种安全措施。 如果此参数设置为“1”,php-fpm将尝试通过查找请求的文件来提供PHP文件,然后如果没有找到完全匹配,它将尝试猜测并返回一个紧密匹配。这是非常不安全的,因为它可以允许您的网站向外部用户暴露敏感信息。 对于php-fpm来说,如果文件与请求的文件完全匹配,则返回更安全,否则返回错误给用户。

接下来,我们需要修改另一个PHP配置文件,以便我们的处理器能够正确连接到我们的Web服务器。

vi /etc/php/7.0/fpm/pool.d/www.conf

找到listen =指令定义(在最后一行),修改为:

listen = /var/run/php/php7.0-fpm.sock

重启php-fpm,可通过service --status-all查看服务全称:

service php7.0-fpm restart

配置mysql

使用命令mysql -u root -p连接mysql,创建数据库zaixianku

create database zaixianku

输入命令exit,退出mysql

配置nginx

删除/etc/nginx/sites-available/default文件。
新建/etc/nginx/conf.d/php.conf文件,填入以下内容:

#php max upload limit cannot be larger than this
client_max_body_size 13m;
index              index.php index.html index.htm;
# Upstream to abstract backend connection(s) for PHP.
upstream php {
	#this should match value of "listen" directive in php-fpm pool
    server unix:/var/run/php/php7.0-fpm.sock;
	#server 127.0.0.1:9000;
}

新建/etc/nginx/global/restrictions.conf文件(多站点公共配置文件),填入以下内容:

# Global restrictions configuration file.
# Designed to be included in any server {} block.
location = /favicon.ico {
    log_not_found off;
    access_log off;
}

location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
    deny all;
}

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}

新建/etc/nginx/global/wordpress.conf文件(wordpress公共配置文件),填入以下内容:

# WordPress single blog rules.
# Designed to be included in any server {} block.
 
# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
    index        index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$args;
}
 
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
 
# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
       access_log off; log_not_found off; expires max;
}
 
# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;
 
# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ \.php$ {
    # Zero-day exploit defense.
    # http://forum.nginx.org/read.php?2,88845,page=3
    # Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
    # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine.  And then cross your fingers that you won't get hacked.
    try_files $uri =404;
 
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
 
    include fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#   fastcgi_intercept_errors on;
    fastcgi_pass php;
}

新建/etc/nginx/sites-enabled/zaixianku.conf,写入以下内容:

server {
    listen 80;
    server_name www.zaixianku.com zaixianku.com;
    root /var/www/zaixianku.com;

    include /etc/nginx/global/restrictions.conf;

    # Additional rules go here.

    # Only include one of the files below.
    include /etc/nginx/global/wordpress.conf;
#   include /etc/nginx/global/wordpress-ms-subdir.conf;
#   include /etc/nginx/global/wordpress-ms-subdomain.conf;
}

到此,nginx配置完毕,执行nginx -t检查配置是否正确,若正确,执行nginx -s reload重载配置。

WordPress配置

nginx默认是用www-data用户,因此更改/var/www/zaixianku.com的所属用户为www-data,所属群组为www-data

chown -R www-data /var/www/zaixianku.com
chgrp -R www-data /var/www/zaixianku.com

输入你的网址,初始化网站。

phpMyAdmin配置

1、打开 /libraries/config.default.php文件(旧版本是根目录下的config.inc.php文件),用写字板(不要用记事本,这是UTF8编码)进行编辑,按照说明配置即可。
2、查找 $cfg['PmaAbsoluteUri']=‘'; // 修改为你将上传到空间的phpMyAdmin的网址
如:$cfg['PmaAbsoluteUri'] =‘http: // 网站域名/phpmyadmin/';
3、查找 $cfg['Servers'][$i]['host'] =‘localhost'; // 通常用默认,也有例外,可以不用修改
4、查找 $cfg['Servers'][$i]['auth_type'] =‘config'; // 在自己的机子里调试用config;如果在网络上的空间用cookie.
在此有四种模式可供选择:cookie,http,HTTP,config
① config 方式即输入phpMyAdmin 的访问网址即可直接进入,无需输入用户名和密码,是不安全的,不推荐使用。
② 设置cookie,http,HTTP方式,登录 phpMyAdmin 需要数据用户名和密码进行验证。
具体如下:PHP 安装模式为 Apache,可以使用 http 和 cookie;PHP 安装模式为 CGI,可以使用 cookie。
5、查找 $cfg['Servers'][$i]['user'] = ‘root'; // MySQL用户名
6、查找 $cfg['Servers'][$i]['password'] =''; // MySQL 密码 (only needed 留空就可以了)
7、查找 $cfg['Servers'][$i]['only_db'] = ''; // 你只有一个数据就设置一下,设置为你的数据库名;如果你想架设服务器,那么建议留空
8、查找 $cfg['DefaultLang'] = ‘zh'; // 这里是选择语言,zh代表简体中文的意思
9、查找$cfg['blowfish_secret'] =''; // 如果认证方法设置为cookie,就需要设置短语密码,设置为什么密码,由您自己决定,这里不能留空,否则会在登录 phpMyAdmin 时提示如下图所示的错误。
配置完成后,使用http://网站域名/phpmyadmin即可访问了

posted @ 2017-07-15 21:40  丐帮杨公子  阅读(455)  评论(0编辑  收藏  举报