美文网首页
Nginx学习笔记-搭建wordpress[不定期更新]

Nginx学习笔记-搭建wordpress[不定期更新]

作者: jessica1123 | 来源:发表于2017-08-31 15:50 被阅读116次

我最近在学习使用Nginx,想搭建并运维一个网站,主要参考了——安全人员学习笔记——Web中间件之Nginx篇这篇文章,但是后续学习和使用,我会记录下自己的学习过程和感受。

首先是安装Nginx

这个比较好安装,只要先配置好源,推荐使用最新的阿里源(后面会讲原因)

deb http://mirrors.aliyun.com/ubuntu/ xenial main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/xenial main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main multiverse restricted universe

上面的是我现在用的比较新的源,如果用旧的源安装的时候会出现很多问题。
然后输入

sudo apt-get install nginx

安装好Nginx之后,可以尝试去用浏览器访问localhost,看到下面界面表示安装成功

img.png

值得一提的是,在配置nginx开机启动时,由于我的系统是ubuntu,因此没有chkconfig命令,但是ubuntu下面有类似的程序—— sysv-rc-conf

sudo apt-get install sysv-rc-conf
sysv-rc-conf --level 35 nginx on  

安装&配置mysql

做这个的时候我花费了很多时间,其实主要的原因就是因为源的版本太老旧了,因此出现了很多问题。在配置好最新的源后,可以尝试

sudo apt-get install mysql-server
apt-get isntall mysql-client
sudo apt-get install libmysqlclient-dev

直接安装mysql,如果出现问题,则可以参考这篇文章—— Linux(Ubuntu16.04)+MySQL Community Server 5.7.17安装(使用MySQL5.7deb安装包)
顺便一提,我用的是上面教程里的方式去安装的,当时遇到的主要问题就是在最后要安装mysql-community-server_5.7.17-1ubuntu16.04_amd64.deb时出现了问题,使用apt-get -f install 却没有解决,原因就是因为在第一步时没有修改源,使用的是之前配置的旧的源,在更新了源之后,update一下,就可以成功安装了。

安装&配置php

做这个也花了我很多时间去调试Nginx的配置文件,使其能够解析php代码,之前freebuf上的教程有很多的坑,下面讲讲我在ubuntu上配置的过程,可以参考——Ubuntu 16.04安装Linux, Nginx, MySQL, PHP (LEMP)

安装php

apt-get -y install php7.0-fpm php-mysql
vim /etc/php/7.0/fpm/php.ini
#打开之后,找到 ";cgi.fix_pathinfo=1"  修改成"cgi.fix_pathinfo=0"
service php7.0-fpm start
#启动php-fpm服务

接着修改/etc/nginx/sites-available/default配置文件,实现php代码的解析(不然访问的时候会直接下载php文件)

cd /etc/nginx/sites-available
cp default default.bak   #先做一下备份
sudo gedit default    #打开default文件进行修改
img.png

此时,只要找到到文件中

# Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;   #在此处添加index.php

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    # 把下面的注释符号#都去掉
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;      #此行需要删除,或者保持#符号注释掉,不然nginx服务会出问题
    #   # With php7.0-fpm:
    #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    #}

修改完文件之后保存,输入

nginx -t

测试一下文件是否都正常,然后重启nginx

service nginx restart

添加一个phpinfo.php文件到/var/www/html/目录

cd /var/www/html
gedit phpinfo.php

添加内容到文件

<?php
    phpinfo();
?>

保存之后,到浏览器里输入localhost/phpinfo.php访问试试,如果出现下图则配置成功。

img.png

说下我当时遇到的问题:
1.freebuf教程一开始误导我网页文件默认是在/usr/share/nginx/html/目录下面,实际是在/var/www/html/目录下面
2.还是freebuf教程误导(不知道是不是nginx版本的原因),说是要修改/etc/nginx/nginx.conf文件,但实际上我无论怎么修改这个文件(当然可能是我技术不够的原因),在网上找了好多个文件的内容都不行,然后发现只需要改/etc/nginx/sites-available/default文件就可以了
3.这个是我个人的原因,就是忘记删除/etc/nginx/sites-available/default文件中
fastcgi_pass 127.0.0.1:9000;
不过好在用nginx -t测试之后,发现错误在这,然后参照简书上的教程,发现需要删除这一行。

4.如过在访问php文件的时候,遇到502错误,可能是因为你没有启动php7.0-fpm,启动之后再试试

安装&配置phpmyadmin

1.安装

apt-get update
apt-get install phpmyadmin php-mbstring php-gettext

2.创建链接

ln -s /usr/share/phpmyadmin/  /var/www/html/

3.重启nginx

systemctl restart nginx

4.访问&登录

http://server_domain_or_IP/phpmyadmin
img.png

注:此处基本没遇到什么坑,需要注意的是安装phpmyadmin的过程中需要选择使用的中间件的类型,如果是Apache的话就选择Apache,此处选择lighthttpd,还有就是需要配置mysql的密码,正常配置就行。

部署wordpress博客

  • 事前准备
    1.下载wordpress压缩包,请自行去官网下载
    2.新建一个用户名和数据库
$:mysql -uroot -p
#先登录root用户,然后执行一下操作
mysql>create database wordpress;
mysql>grant all privileges on wordpress.* to username@localhost identified by 'password';    #此处的username&password自己设置
#刷新系统权限表
mysql>flush privileges;
  • 复制站点
tar -zxvf wordpress.xxx.tar.gz
mv wordpress /var/www/html/

复制完之后,有时候想设置wordpress目录作为nginx服务的根目录,那必须在安装wordpress之前就修改相关的配置文件,否则wordpress安装过程中会默认写路径到相关的php文件里,之后要重新配置的话就需要修改wordpress下面的php文件了,很是麻烦,所以我们事先修改,之后再安装,这样用localhost访问到的就是wordpress了,这样也能更好的部署域名

vim /etc/nginx/sites-enabled/default

找到

root /var/www/html;

改成

root /var/www/html/wordpress;

保存之后重启nginx

  • 安装wordpress
    此处比较简单,先修改wordpress目录的权限
chown www-data /var/www/html/wordpress/

之后的安装流程请参考我之前写的文章——Apache2+php7+mysql搭建wordpress
里面的安装部分

  • 接下来说一下wordpress在nginx下面的固定链接的设置
    打开/etc/nginx/sites-enabled/default
    找到server{}里面的 location / {}部分,把他修改成
location / {
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;
}
}

保存之后再重启nginx,应该就可以设置固定链接了

相关文章

网友评论

      本文标题:Nginx学习笔记-搭建wordpress[不定期更新]

      本文链接:https://www.haomeiwen.com/subject/asxtjxtx.html