11 搭建博客

引言

1.浏览器拿着域名去DNS服务器,DNS服务器解析域名返回IP
2.浏览器拿着IP与nginx服务器建立TCP连接
3.建立连接以后,发起请求(GET、post)
4.nginx判断请求是动态还是静态
	#静态
	location / {
		root /code;
		index index.html;
	}
	
	#动态
	location ~* .php$ {
		fastcgi_pass 127.0.0.1:9000;
	}
5.如果是静态请求,nginx直接返回
6.如果是动态请求,nginx会通过fastcgi协议将请求发给php-fpm管理进程
7.php-fpm管理进程将请求下发给wrapper工作进程
8.wrapper进程判断php内容是否可以直接解析结果
9.如果可以直接解析,则返回内容
10.如果需要连接数据库等获取数据,则wrapper会去数据库获取数据,再返回数据

安装Nginx

[root@web1 ~]# cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[root@web1 ~]# yum clean all
[root@web1 ~]# yum makecache
[root@web1 ~]# yum install nginx -y

安装php

[root@VM-0-14-centos nginx]#/etc/yum.repo.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

[root@VM-0-14-centos nginx]# yum remove php-mysql-5.4 php php-fpm php-common
[root@VM-0-14-centos nginx]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71wxml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71wpecl-redis php71w-pecl-mongodb

#统一用户
[root@web01 ~]# vim /etc/php-fpm.d/www.conf	#改成group和user改成www(没有的话就添加www组和www用户)

#启动并验证
[root@web01 ~]# systemctl start php-fpm.service 
[root@web01 ~]# ps -ef | grep php
root       1925      1  4 20:20 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
www        1926   1925  0 20:20 ?        00:00:00 php-fpm: pool www
www        1927   1925  0 20:20 ?        00:00:00 php-fpm: pool www
www        1928   1925  0 20:20 ?        00:00:00 php-fpm: pool www
www        1929   1925  0 20:20 ?        00:00:00 php-fpm: pool www
www        1930   1925  0 20:20 ?        00:00:00 php-fpm: pool www
root       1935   1879  0 20:20 pts/0    00:00:00 grep --color=auto php

安装Mariadb

#安装mariadb-server
[root@web01 conf.d]# yum install mariadb-server -y

#配置开机启动
[root@web01 conf.d]# systemctl start mariadb.service 
[root@web01 conf.d]# systemctl enable mariadb.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

#创建用户并登录注册
[root@web01 conf.d]# mysqladmin -uroot password 123
[root@web01 conf.d]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 4
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> 

#创建数据库
[root@web01 conf.d]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 5
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)


#php关联数据库
[root@web01 code]# vim mysql.php	#站点目录下创建
<?php
    $servername = "localhost";
    $username = "root";
    $password = "123";

    // 创建连接
    $conn = mysqli_connect($servername, $username, $password);

    // 检测连接
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "小哥哥,php可以连接MySQL...";
?>

<img style='100%;height:100%;' src=https://www.mumusir.com/picture/php_mysql.png>

上传代码

#创建目录授权
[root@web01 ~]# mkdir /nginx_data
[root@web01 ~]# rz
[root@web01 ~]# chown -R www.www /nginx_data/

编写Nginx配置

server {
	root /nginx_data/wordpress;	#指定站点目录
	listen 80;	#监听80端口
	server_name wpdboke.com;	#域名

	location / {
	index index.php;	#索引目录文件
	}

	location ~* .php$ {
		fastcgi_pass 127.0.0.1:9000;
        	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

	}	#Nginx关联php

	location =/status {
		auth_basic           "closed site";
		auth_basic_user_file /etc/nginx/passwd;
		stub_status;
		allow 192.168.0.0/24;
		deny all;
	}	#后台状态需要密码登录,只允许特定网段的的需求



	location /wp-content {
		autoindex on;
		autoindex_localtime on;
		autoindex_exact_size on;

	}	#将wp-content做为站点目录

}

优化

#报错是413,原因是文件过大
#Nginx
#解决:
[root@web01 code]# vim /etc/nginx/nginx.conf
http{
	... ...
	client_max_body_size 100m;
	... ...
}

#报错为405,因为nginx解析不了php文件,只能报错


#php
[root@web01 code]# vim /etc/php.ini
post_max_size = 200M
upload_max_filesize = 200M

花里胡哨


        # 配置设置图片格式文件
        location ~* .(jpg|jpeg|gif|png|ico|swf)$ {
                # 过期时间为3年
                expires 3y;
                # 关闭日志记录
                access_log off;
                # 关闭gzip压缩,减少CPU消耗,因为图片的压缩率不高。
                gzip off;
        }
        # 配置css/js文件
        location ~* .(css|js)$ {
                access_log off;
                expires 3y;
        }
        # 禁止用户上传目录下所有.php文件的访问,提高安全性
        location ~ ^/files/.*.(php|php5)$ {
                deny all;
        }

        # 以下配置允许运行.php的程序,方便于其他第三方系统的集成。
        location ~ .php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_split_path_info ^(.+.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param  HTTPS              off;
        }
}
原文地址:https://www.cnblogs.com/zhaokunhao/p/14716824.html