安装wordpress, 安装discuz, 域名重定向, 用户认证, nginx访问日志

安装worrdpress

  • 从wordpress官网找到下载链接,下载到linux服务器上;
  • 解压缩,把数据放到网站的根目录下;
  • 设置虚拟主机配置文件;
cd /usr/local/src
wget https://cn.wordpress.org/wordpress-5.0.2-zh_CN.tar.gz
tar zxvf workpress-5.0.2-zh.CN.tar.gz
mv wordpress-5.0.2-zh.CN/*  /data/wwwroot/blog.tany.com/
vi /etc/nginx/blog.tany.conf

虚拟主机配置文件如下:

server {
    listen       80 default_server;
    server_name blog.tany.com;

	root	/data/wwwroot/blog.tany.com/;
    index  index.html index.htm index.php;

 location ~ \.php$ {
        root           /data/wwwroot/blog.tany.com;
        fastcgi_pass   127.0.0.1:9000;     #将请求交给php解释器php-fpm;
        fastcgi_index  index.php;       #大概跟上面index一个意思,进入目录,先查找index.php;
        fastcgi_param  SCRIPT_FILENAME  /data/wwwroot/blog.tany.com$fastcgi_script_name;   #设置请求中的参数,感觉是将nginx使用的变量跟PHP使用的变量对应起来;下面的include应该也是变量设置;
        include        fastcgi_params;     #可查看文件/etc/nginx/fastcgi_params(yum安装nginx);
    }
}
  • 设置这个网站的数据库
mysql -uroot	#登陆数据库不用密码;
create database blog;	#创建一个blog库;
grant all on blog.* to 'blog'@'127.0.0.1' identified by 'randompw';	#授权一个新用户blog拥有blog库所有表的权限,密码为最后一段,最好随机生成;
exit
  • 登录网站blog.tany.com(客户机要设置好hosts文件,让测试域名指向服务器IP);
    在这里插入图片描述

  • 提交后提示没有权限
    在这里插入图片描述

  • 要修改目录的权限;

chown -R php-fpm /data/wwwroot/blog.tany.com
  • 再次提交即可成功连续数据库,设置站点基础信息;

安装discuz

  • 从discuz官网找到下载链接,使用git clone下载到linux服务器上;
    找到产品发布页,点击进入git链接
    在这里插入图片描述
    git链接里右边复制https地址
    在这里插入图片描述
cd /usr/local/src
yum install -y git
git clone https://gitee.com/ComsenzDiscuz/DiscuzX.git
cp -r DiscuzX/upload /data/wwwroot/bbs.tany.com #复制和修改目录名;
  • 设置虚拟主机配置文件;
vi /etc/nginx/bbs.tany.conf

虚拟主机配置文件如下:

server {
    listen       80 ;
    server_name bbs.tany.com;

	root	/data/wwwroot/bbs.tany.com/;
    index  index.html index.htm index.php;

 location ~ \.php$ {
        root           /data/wwwroot/bbs.tany.com;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /data/wwwroot/bbs.tany.com$fastcgi_script_name;
        include        fastcgi_params;
    }
}
  • 设置这个网站的数据库
mysql -uroot	#登陆数据库不用密码;
create database bbs;	#创建一个bbs库;
grant all on bbs.* to 'bbs'@'127.0.0.1' identified by 'randompw';	#授权一个新用户bbs拥有bbs库所有表的权限,密码为最后一段,最好随机生成;
exit
  • 登录网站bbs.tany.com(客户机要设置好hosts文件,让测试域名指向服务器IP);

在这里插入图片描述

  • 目录没有写权限,要修改权限;
cd /data/wwwroot/bbs.tany.com
chown -R php-fpm config data uc_server/data uc_client/data
  • 填写数据库相关信息和管理员用户名密码,即可登录成功
    在这里插入图片描述

域名重定向

  • 配置虚拟主机可以让两个域名访问同一个目录(站点数据),但是域名没有主次,如果要增加其中一个域名的权重,可以做域名重定向;
  • 例如:以域名2为主,域名1是曾经用过的,带流量,将域名1定向到域名2;
  • URL/URI代表链接域名后的部分
  • wordpress里面有一个设置是"使用域名的设置",无论以什么域名访问到主页,页面上其他链接都是以"使用域名”为开头;

重写从http开始
如果一个URI匹配指定的正则表达式regex,URI就按照replacement重写。

  • 域名得定向语句
if ( $host = blog.aminglinux.cc )
    {
    	rewrite /(.*)  http://www.aming.com/$1 permanent;
	    }

上面语句尝试不行,可能是nginx版本问题;
最新可行语句:

server {
    listen       80 ;
    server_name www.tany.com blog.tany.com;
       if ( $host = www.tany.com ) {
        rewrite "^/(.*)$"  http://blog.tany.com/$1 permanent;
        }   

状态码301,如果要增加权重,域名rewrite, 要加permanent;

  • 临时重定向,状态码302
rewrite /1.txt /2.txt redirect;

用户认证

  • 有些浏览器没办法显示认证页面,如qq,测试用safari;

  • 可以全网需要认证,语名如下:

       location / {

            root   html;

            index  index.html index.htm;

            auth_basic "User Authentication";              ## 认证弹窗提示语

            auth_basic_user_file /etc/nginx/user_passwd;		#安装httpd-tools,命令htpasswd -c /etc/nginx/user_passwd user1创建第一个用户,再加用户的命令是htpasswd -m /etc/nginx/user_passwd user2;

        }
  • 如局部需要认证
    要定义全局根目录,把root, index语句从location语句里搬出来;
root   /data/wwwroot/bbs.tany.com;	
	index  index.html index.htm index.php;	
	
location /jiami/      
       {
       auth_basic "User Authentication";      
       auth_basic_user_file /etc/nginx/user_passwd;    
       }
  • 局部测试结果,curl命令和-u选项;
[root@draft conf.d]# curl -x127.0.0.1:80   blog.tany.com/jiami/s1.txt -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.16.1
Date: Mon, 14 Oct 2019 02:49:08 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
WWW-Authenticate: Basic realm="User Authentication"

[root@draft conf.d]# curl -x127.0.0.1:80 -uuser1:123456  blog.tany.com/jiami/s1.txt -I
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Mon, 14 Oct 2019 02:49:22 GMT
Content-Type: text/plain
Content-Length: 14
Last-Modified: Sun, 13 Oct 2019 13:37:58 GMT
Connection: keep-alive
ETag: "5da328b6-e"
Accept-Ranges: bytes
  • php链接需要用户认证,要把php解释语句也话在认证语句下;
root    /data/wwwroot/bbs.tany.com/;
        index  index.html index.htm index.php;
location ~ /admin.php       
       {
       auth_basic "User Authentication";     
       auth_basic_user_file /etc/nginx/user_passwd;    
 fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /data/wwwroot/bbs.tany.com$fastcgi_script_name;
        include        fastcgi_params;
       }
  • php链接用户认证测试结果:
[root@draft conf.d]# curl -x127.0.0.1:80 bbs.tany.com/admin.php -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.16.1
Date: Mon, 14 Oct 2019 03:14:32 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
WWW-Authenticate: Basic realm="User Authentication"

[root@draft conf.d]# curl -x127.0.0.1:80 -uuser1:123456 bbs.tany.com/admin.php -I
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Mon, 14 Oct 2019 03:14:48 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
X-Powered-By: PHP/7.3.0
Set-Cookie: hHen_2132_saltkey=c5j6yjU7; expires=Wed, 13-Nov-2019 03:14:48 GMT; Max-Age=2592000; path=/; HttpOnly
Set-Cookie: hHen_2132_lastvisit=1571019288; expires=Wed, 13-Nov-2019 03:14:48 GMT; Max-Age=2592000; path=/
Set-Cookie: hHen_2132_sid=c335Pu; expires=Tue, 15-Oct-2019 03:14:48 GMT; Max-Age=86400; path=/
Set-Cookie: hHen_2132_lastact=1571022888%09admin.php%09; expires=Tue, 15-Oct-2019 03:14:48 GMT; Max-Age=86400; path=/

nginx访问日志

  • 默认设置/etc/nginx/nginx.conf
    定义了日志是以什么格式记录的,虚拟主机里再增加的日志也是这种格式;
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

log_format main每一个变量的意思为:访问服务器的IP地址,访问的用户(认证后才有),访问时间,请求(方法,链接,http版本),状态码,数据量,referer,浏览器相关信息,代理用户;

  • nginx默认有访问日志access_log,但是想把访问日志按虚拟主机配置分开,就要在每个虚拟主机配置里面单独配置;
    修改配置文件后,自动生成,目录要存在;

  • 虚拟主机配置日志的语句

access_log /data/logs/blog.access.log main; #写在配置文件最后一行;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值