Linux网站搭建

1 回顾中间件

1
2
3
4
5
6
7
8
9
10
11
中间件 = 平台 + 通信

NGINX、Apache、IIS、Tomcat

IIS:互联网信息服务(Internet Information Services,简称IIS),是由微软公司提供的基于运行Microsoft Windows的互联网基本服务。

Apache:( Apache HTTP Server)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。

Nginx:Nginx是一个高性能的HTTP和反向代理web服务器继Apache之后的另一款在Linux下被大量使用的web服务软件

Tomcat: Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。
1
2
3
4
- 常见的网站框架
LNMP/LAMP/WISP
- 常见的网站架构
单机架构、集群架构、分布式架构

2 Nginx概念及部署

1
2
3
• 稳定性和低系统资源损耗,并发连接的高处理能力
• 一台物理服务器可处理30000~50000个并发请求
• 可以在大多数 Unix Linux OS 上编译运行,并有 Windows 移植版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
- 官网
https://nginx.org/en/download.html

+ 下载nginx的源码
wget https://nginx.org/download/nginx-1.28.0.tar.gz
tar -zxf nginx-1.28.0.tar.gz
+ 安装依赖环境
yum -y install gcc-c++ pcre-devel zlib-devel
- 网络源下载
- gcc-c++ C语言编译环境
- pcre-devel zlib-devel 开发包/资源
+ 开始安装源码
./configure --- 默认路径 /usr/local/ --- 修改路径 "--prefix=路径"
make&&make install --- 编译及安装
+ nginx的目录
cd /usr/local/nginx/
conf 配置文件 html 根目录 logs 日志文件 sbin 二进制文件
+ 启用nginx
./sbin/nginx
- 防火墙

+ 导入nginx的命令
vim /etc/profile
export NGINX_PATH=/usr/local/nginx/sbin
export PATH=$PATH:$NGINX_PATH
source /etc/profile
+ 使用命令
nginx -v 查看版本
nginx -s stop/quit 关闭nginx服务
nginx -s reload/reopen 重启nginx服务
- nginx重启是需要nginx开启的状态下才能重启

3 php部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 提前关闭Firewalls和selinux
- 默认网络源并不是什么东西都有 --- 需要额外的资源/仓库来获取软件
wget https://archives.fedoraproject.org/pub/archive/epel/7/x86_64/Packages/e/epel-release-7-14.noarch.rpm
yum -y install epel-release-7-14.noarch.rpm
rpm -Uvh https://repo.webtatic.com/yum/el7/webtatic-release.rpm
- 加密且更新下载web服务资源仓库
- yum makecache 重新加载

+ 安装php服务及拓展
yum -y install php72w-cli php72w-common php72w-devel php-mysqli php72w-fpm php72w php72w-cli php72w-fpm php72w-curl php72w-gd php72w-mysql php72w-xml php72w-mbstring
+ 确定php是否安装成功
php -v
PHP 7.2.34 (cli) (built: Oct 1 2020 13:37:37) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

+ 开启短标签识别
vim /etc/php.ini
short_open_tag = On
+ 开启服务
systemctl start php-fpm.service
- 9000端口是否开启

4 nginx和php配合操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
+ php文件能够nginx进行识别
vim index.php
<?php
phpinfo();
?>

+ nginx配置文件的修改
vim /usr/local/nginx/conf/nginx.conf
- http模块
http {
include mime.types; //文件扩展名与文件类型映射表
default_type application/octet-stream; //默认文件类型
sendfile on; //支持文件发送(下载)
keepalive_timeout 65; //连接保持超时时间
server { //web服务监听配置
listen 80; //监听端口
server_name localhost; //网站名称
location / { //定义安装目录为软件根目录
root html; //网页存放目录
index index.php index.html index.htm; //默认索引网页
}
error_page 500 502 503 504 /50x.html; //错误页面
location = /50x.html { //错误页面配置
root html;
}
- php模块 --- 取消注释
location ~ \.php$ {
root html; 根目录
fastcgi_pass 127.0.0.1:9000; 监听
fastcgi_index index.php; 首页
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 网站根目录
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; nginx的根目录文件位置
include fastcgi_params;
}
}
}

+ 重启服务 --- nginx和php

5 mysql部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- 防火墙和selinux关闭
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar
tar -xf mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar

- MYSQL
https://www.runoob.com/mysql/mysql-tutorial.html

+ 安装
yum -y remove mariadb-libs
rpm -ivh mysql-community-server-8.0.30-1.el7.x86_64.rpm mysql-community-client-8.0.30-1.el7.x86_64.rpm mysql-community-client-plugins-8.0.30-1.el7.x86_64.rpm mysql-community-icu-data-files-8.0.30-1.el7.x86_64.rpm mysql-community-common-8.0.30-1.el7.x86_64.rpm mysql-community-libs-8.0.30-1.el7.x86_64.rpm mysql-community-libs-compat-8.0.30-1.el7.x86_64.rpm

+ 使用
systemctl start mysqld 启动mysql

+ 简单的数据语句
mysql -u root -p
- 密码 --- grep "password" /var/log/mysqld.log
ALTER user 'root'@'localhost' IDENTIFIED BY 'Aa@123456';
- 初始密码

show databases; 查看数据库
use mysql; 选择数据库
show tables; 选择表

select 字段 from 表; 查询语句
- 前提是已经选择数据库的情况下
select 字段 from 数据库.表; 查询语句
- 不管有没有选择数据库都可以查看
select user,host from mysql.user;
- 查看数据库服务的登录用户 和 主机名

6 nginx和php和mysql配合操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
+ 创建数据库测试连接的php文件
vim mysql.php
<?php
$servername = "MYSQL服务器IP";
$username = "用户名";
$password = "密码";
$conn = mysqli_connect($servername,$username,$password);
if($conn->connect_error){
die("数据库连接失败: ".$conn->connect_error);
}
echo "数据库连接成功";
phpinfo();
?>


+ 如何确定数据库是否真的与站点形成联动?
- mysqli函数的调用是否成功? --- active links 是否为1 1成功 0失败

vim /etc/php.ini
display_errors = On
- 开启php的报错信息

- 排错
Warning: mysqli_connect(): (HY000/1130): Host '192.168.137.136' is not allowed to connect to this MySQL server in /usr/local/nginx/html/mysql.php on line 5
数据库无法正常连接

- 修改数据库服务用户的host
update mysql.user set host='%' where user='root'; //将root用户的权限改成任何主机都能访问
flush privileges; //刷新权限设置


Warning: mysqli_connect(): (HY000/2059): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory in /usr/local/nginx/html/mysql.php on line 5
身份认证模块不匹配

alter user 'root'@'%' identified with mysql_native_password by 'Aa@123456'; //修改身份验证插件


+ 最后要查看
- mysqli函数的调用是否成功? --- active links 是否为1 1成功 0失败

7 导入源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
tar -xf PHPCMS.tar
- 源码需要放置根目录
+ 导入源码要先尝试访问,通过报错进行配置
http://192.168.137.136/PHPCMS/
- 思路
1.对方的源码的数据库连接文件
vim includes/db.php
2.对方的源码数据库文件是否导入数据库
create database cms;
use cms;
source /home/cms.sql


+ 访问网站发现网站样式不好看
- 当前网络环境无法访问到 外网链接的css样式
1
2
3
4
- 数据库的root用户的host为%是一个危险操作
+ 创建一个用户,赋予权限,修改host为对应站点IP,修改身份验证
+ root用户host改回localhost
+ 数据库连接配置文件,就得按照新用户的信息填写,用户名demo 密码Aa@123456