一 Apache网站服务
1.1 Apache简介
1.1.1 Apache起源
● 源于A Patchy Server,著名的开源web服务软件
● 1955年时,发布Apache服务程序的1.0版本
● 由Apache软件基金会(ASF)负责维护
● 最新的名称为“Apache HTTP Server”
1.1.2 主要特点
● 开放源代码,跨平台应用
● 支持多种网页编程语言
● 模块化设计,运行稳定,良好的安全性
1.2 编译安装httpd服务器
1.2.1将安装Apache所需软件上传到opt目录下
[root@localhost opt]# ll
total 81640
-rw-r--r-- 1 root root 1071074 Aug 31 14:52 apr-1.6.2.tar.gz
-rw-r--r-- 1 root root 565507 Aug 31 14:52 apr-util-1.6.0.tar.gz
-rw-r--r-- 1 root root 6567926 Aug 31 14:52 httpd-2.4.29.tar.bz2
1.2.2 源码编译及安装
[root@localhost opt]# tar xf apr-1.6.2.tar.gz
[root@localhost opt]# tar xf apr-util-1.6.0.tar.gz
[root@localhost opt]# tar xf httpd-2.4.29.tar.bz2
[root@localhost opt]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
[root@localhost opt]# yum -y install gcc gcc-c++ make pcre-devel expat-devel perl
[root@localhost opt]# cd /opt/httpd-2.4.29/
[root@localhost opt]# ./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
[root@localhost httpd-2.4.29]# make && make install
1.2.3 确认安装结果
[root@localhost httpd-2.4.29]# ls /usr/local/httpd/
bin cgi-bin error icons lib man modules
build conf htdocs include logs manual
1.2.4 优化执行路径
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@localhost httpd-2.4.29]# httpd -v
Server version: Apache/2.4.29 (Unix)
Server built: Aug 31 2020 15:05:47
1.2.5 添加httpd系统服务
[root@localhost httpd-2.4.29]# cd /lib/systemd/system
[root@localhost system]# vi httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart= /usr/local/bin/apachectl $OPTIONS
ExecrReload= /bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl start httpd.service
[root@localhost system]# systemctl enable httpd.service
[root@localhost system]# systemctl is-enabled httpd.service
enabled
二 httpd服务器基本配置
2.1 Web站点部署过程
2.1.1 确定网站名字,IP地址
[root@localhost ~]# hostname www.51xit.top
[root@localhost ~]# vi /etc/hostname
localhost.localdomain
www.51xit.top
[root@localhost ~]# bash
[root@www ~]#
2.1.2 配置并启动httpd服务
[root@localhost system]# vi /usr/local/httpd/conf/httpd.conf
ServerName www.51xit.top:80
[root@www /]# /usr/local/httpd/bin/apachectl -t
Syntax OK
[root@www /]# systemctl restart httpd.service
[root@www /]# netstat -anpt | grep httpd
tcp6 0 0 :::80 :::* LISTEN 93442/httpd
2.1.3 部署网页文档
[root@www /]# cat /usr/local/httpd/htdocs/index.html
It works!
### 2.1.4 在客户机中访问Web站点 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200831153317917.png#pic_center) ### 2.1.5 查看Web站点的访问情况[root@www /]# tail /usr/local/httpd/logs/access_log
20.0.0.1 - - [31/Aug/2020:15:23:04 +0800] "GET / HTTP/1.1" 200 45
20.0.0.1 - - [31/Aug/2020:15:23:04 +0800] "GET /favicon.ico HTTP/1.1" 404 209
20.0.0.1 - - [31/Aug/2020:15:23:22 +0800] "GET /favicon.ico HTTP/1.1" 404 209
[root@www /]# tail /usr/local/httpd/logs/error_log
[Mon Aug 31 15:14:48.484522 2020] [mpm_event:notice] [pid 92840:tid 139827623069504] AH00489: Apache/2.4.29 (Unix) configured -- resuming normal operations
[Mon Aug 31 15:14:48.484649 2020] [core:notice] [pid 92840:tid 139827623069504] AH00094: Command line: '/usr/local/httpd/bin/httpd'
[Mon Aug 31 15:21:18.484878 2020] [mpm_event:notice] [pid 92840:tid 139827623069504] AH00491: caught SIGTERM, shutting down
[Mon Aug 31 15:21:18.519121 2020] [mpm_event:notice] [pid 93106:tid 139741371254592] AH00489: Apache/2.4.29 (Unix) configured -- resuming normal operations
[Mon Aug 31 15:21:18.519226 2020] [core:notice] [pid 93106:tid 139741371254592] AH00094: Command line: '/usr/local/httpd/bin/httpd'
[Mon Aug 31 15:30:40.913905 2020] [mpm_event:notice] [pid 93106:tid 139741371254592] AH00491: caught SIGTERM, shutting down
[Mon Aug 31 15:30:40.950866 2020] [mpm_event:notice] [pid 93442:tid 140610428782400] AH00489: Apache/2.4.29 (Unix) configured -- resuming normal operations
[Mon Aug 31 15:30:40.950989 2020] [core:notice] [pid 93442:tid 140610428782400] AH00094: Command line: '/usr/local/httpd/bin/httpd'
2.2 httpd.conf配置文件
2.2.1 全局配置项
2.2.2 常用的全局配置参数
2.2.3 区域配置项
三 构建虚拟Web主机
3.1 基于域名的虚拟主机
[root@www ~]# mkdir -p /opt/www/html/51xit.top
[root@www ~]# mkdir -p /opt/www/html/52xit.top
[root@www ~]# echo "<h1>www.51xit.top</h1>" > /opt/www/html/51xit.top/index.html
[root@www ~]# echo "<h1>www.52xit.top</h1>" > /opt/www/html/52xit.top/index.html
[root@www ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:80> ####设置 51xit.top 虚拟站点区域
DocumentRoot "/opt/www/html/51xit.top"
ServerName www.51xit.top
ErrorLog "logs/www.51xit.top.error_log"
CustomLog "logs/www.51xit.top.access_log" common
<Directory "/opt/www/html"> ####设置目录访问权限
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80> ####设置 52xit.top 虚拟站点区域
DocumentRoot "/opt/www/html/52xit.top"
ServerName www.52xit.top
ErrorLog "logs/www.52xit.top.error_log"
CustomLog "logs/www.52xit.top.access_log" common
<Directory "/opt/www/html"> ####设置目录访问权限
Require all granted
</Directory>
</VirtualHost>
[root@www html]# vi /usr/local/httpd/conf/httpd.conf
#ServerName www.51xit.top:80
Include conf/extra/httpd-vhosts.conf
[root@www html]# systemctl restart httpd.service
用真机测试:
- 找到真机的host文件: C:\WINDOWS\System32\drivers\etc
- 修改文件权限,具体方法见: https://jingyan.baidu.com/article/624e7459b194f134e8ba5a8e.html
- 添加映射关系
- 网页测试
3.2 基于IP地址的虚拟主机
- 添加另外一块网卡VM1 配置ens36网卡
[root@www ~]# nmcli connection
NAME UUID TYPE DEVICE
ens33 1949bc79-ed15-495e-b4a8-86fcd308122c ethernet ens33
virbr0 63bdc0ab-06ea-49c0-b1dc-05b75cca0a91 bridge virbr0
Wired connection 1 ebcd01a6-103c-3398-b3eb-1d1f11feff1a ethernet ens36
[root@www ~]# cd /etc/sysconfig/network-scripts/
[root@www network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[root@www network-scripts]# vi ifcfg-ens36
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens36
UUID=ebcd01a6-103c-3398-b3eb-1d1f11feff1a
DEVICE=ens36
ONBOOT=yes
IPADDR=192.168.100.21
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
DNS1=8.8.8.8
[root@www network-scripts]# systemctl restart network
[root@www network-scripts]# ifdown ens36
Device 'ens36' successfully disconnected.
[root@www network-scripts]# ifup ens36
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/11)
[root@www network-scripts]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 20.0.0.21:80>
ServerAdmin admin@51xit.top
DocumentRoot "/opt/www/html/51xit.top"
ServerName www.51xit.top
ErrorLog "logs/www.51xit.top.error_log"
CustomLog "logs/www.51xit.top.access_log" common
<Directory "/opt/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.100.21:80>
ServerAdmin admin@52xit.top
DocumentRoot "/opt/www/html/52xit.top"
ServerName www.52xit.top
ErrorLog "logs/www.52xit.top.error_log"
CustomLog "logs/www.52xit.top.access_log" common
<Directory "/opt/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@www network-scripts]# systemctl restart httpd
[root@www network-scripts]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
20.0.0.21 www.51xit.top
192.168.100.21 www.52xit.top
-
真机修改下映射关系
-
真机测试
3.3 基于端口的虚拟主机
[root@www ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 20.0.0.21:80>
ServerAdmin admin@51xit.top
DocumentRoot "/opt/www/html/51xit.top"
ServerName www.51xit.top
ErrorLog "logs/www.51xit.top.error_log"
CustomLog "logs/www.51xit.top.access_log" common
<Directory "/opt/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 20.0.0.21:8080>
ServerAdmin admin@52xit.top
DocumentRoot "/opt/www/html/52xit.top"
ServerName www.52xit.top
ErrorLog "logs/www.52xit.top.error_log"
CustomLog "logs/www.52xit.top.access_log" common
<Directory "/opt/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@www ~]# vi /usr/local/httpd/conf/httpd.conf
Listen 20.0.0.21:80
Listen 20.0.0.21:8080
Include conf/extra/httpd-vhosts.conf
[root@www ~]# vi /etc/hosts
20.0.0.21 www.51xit.top
20.0.0.21 www.52xit.top
客户机测试
[root@ns1 ~]# lynx 20.0.0.21:80
[root@ns1 ~]# lynx 20.0.0.21:8080
四 MySQL服务
4.1 MySQL的编译安装
4.1.1 准备工作
[root@localhost ~]# rpm -q mysql-server mysql
package mysql-server is not installed
package mysql is not installed
4.1.2 源码编译及安装
[root@localhost ~]# yum -y install ncurses ncurses-devel bison cmake
[root@localhost ~]# useradd -s /sbin/nologin mysql
[root@localhost ~]# cd /opt/
[root@localhost opt]# tar xf mysql-boost-5.7.20.tar.gz
[root@localhost mysql-5.7.20]# cd mysql-5.7.20/
[root@localhost mysql-5.7.20]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
[root@localhost mysql-5.7.20]# make && make install
4.1.3 安装后的其他调整
[root@localhost mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost mysql-5.7.20]# vi /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
[root@localhost mysql-5.7.20]# chown mysql:mysql /etc/my.cnf
[root@localhost mysql-5.7.20]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@localhost mysql-5.7.20]# echo 'export PATH' >> /etc/profile
[root@localhost mysql-5.7.20]# source /etc/profile
4.1.4 添加系统服务
[root@localhost mysql-5.7.20]# cd /usr/local/mysql/
[root@localhost mysql]# bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@localhost mysql]# systemctl enable mysqld.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@localhost mysql]# systemctl start mysqld.service
[root@localhost mysql]# systemctl status mysqld.service
[root@localhost mysql]# netstat -anpt | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 116631/mysqld
4.2 访问MySQL数据库
4.2.1 登录到MySQL服务器
[root@localhost mysql]# mysqladmin -u root -p password
Enter password:
New password:
Confirm new password:
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@localhost mysql]# mysql -u root -p
Enter password:
4.2.2 执行MySQL操作语句
mysql> SHOW DATABASES;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SHOW DATABASES' at line 6
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
mysql> exit
Bye
五 PHP服务
5.1 安装PHP软件包
5.1.1 安装扩展工具库
[root@localhost ~]# yum -y install \
> libjpeg \
> libjpeg-devel \
> libpng libpng-devel \
> freetype freetype-devel \
> libxml2 \
> libxml2-devel \
> zlib zlib-devel \
> curl curl-devel \
> openssl openssl-devel
5.1.2 编译安装PHP
[root@localhost opt]# cd /opt
[root@localhost opt]# tar xjvf php-7.1.10.tar.bz2
[root@localhost opt]# cd php-7.1.10/
[root@localhost php-7.1.10]# ./configure \
> --prefix=/usr/local/php \
> --with-apxs2=/usr/local/httpd/bin/apxs \
> --with-mysql-sock=/usr/local/mysql/mysql.sock \
> --with-mysqli \
> --with-zlib \
> --with-curl \
> --with-gd \
> --with-jpeg-dir \
> --with-png-dir \
> --with-freetype-dir \
> --with-openssl \
> --enable-mbstring \
> --enable-xml \
> --enable-session \
> --enable-ftp \
> --enable-pdo \
> --enable-tokenizer \
> --enable-zip
[root@localhost php-7.1.10]# make && make install
5.2 设置LAMP组件环境
5.2.1 php.in配置调整
[root@localhost php-7.1.10]# vi /usr/local/php/lib/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock
date.timezone = Asia/Shanghai
[root@localhost php-7.1.10]# /usr/local/php/bin/php -m
5.2.2 httpd.conf配置调整
[root@localhost php-7.1.10]# vi /etc/httpd.conf
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.php index.html
5.3 测试LAMP协同工作
5.3.1 测试PHP网页能否正确显示
[root@localhost php-7.1.10]# rm -f /usr/local/httpd/htdocs/index.html
[root@localhost php-7.1.10]# vi /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>
[root@localhost php-7.1.10]# systemctl restart httpd
5.3.2 测试PHP网页能否访问MySQL数据库
[root@localhost php-7.1.10]# mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@localhost php-7.1.10]# mysql -u root -p
Enter password:
mysql> CREATE DATABASE myadm;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT all ON myadm.* TO 'myadm'@'%' IDENTIFIED BY 'admin123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> GRANT all ON myadm.* TO 'myadm'@'localhost' IDENTIFIED BY 'admin123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@localhost php-7.1.10]# vi /usr/local/httpd/htdocs/index.php
<?php
$link=mysqli_connect('192.168.32.21','myadm','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
六 LAMP架构应用实例
6.1 部署phpMyAdmin系统
6.1.1 解包并复制到网站目录
[root@localhost ~]# cd /opt
[root@localhost opt]# unzip phpMyAdmin-4.7.6-all-languages.zip -d /opt/
[root@localhost opt]# mv phpMyAdmin-4.7.6-all-languages /usr/local/httpd/htdocs/myadm
[root@localhost opt]# cd /usr/local/httpd/htdocs/myadm
[root@localhost myadm]# cp config.sample.inc.php config.inc.php
6.1.2 建立配置文件config.ini.php
[root@localhost myadm]# vi config.inc.php
$cfg['Servers'][$i]['host'] = '127.0.0.1';
6.1.3 访问phpMyAdmin的Web管理界面