3. 实验内容
3.1 Nginx安装和部署
Nginx 是一款轻量级的 Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。
本实验要求安装和部署 web 服务器 Nginx,实现最基本的静态网页和动态网页访问。网页内容不限。
参考资料:
https://nginx.org/en/download.html
https://blog.csdn.net/qq_33454884/article/details/89212702
3.2 Apache安装和部署
Apache 是最流行的 Web 服务器端软件之一。它快速、稳定。
本实验要求安装和部署 web 服务器 Apache,实现最基本的静态网页和动态网页访问。网页内容不限。
参考资料:
https://www.apache.org/
https://blog.csdn.net/weixin_42709659/article/details/81938176
4、实验结果与分析
4.1 Nginx安装和部署
4.1.1 安装Nginx
4.1.1.1 添加源
默认情况Centos7中无Nginx的源,要执行以下命令添加源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
4.1.1.2 安装Nginx
通过以下命令查看是否加源成功
yum search nginx
下载nginx
yum install -y nginx
4.1.2 检测Nginx是否安装成功
nginx -t
4.1.3 启动nginx
sudo systemctl start nginx
出现报错
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
输入命令,查看服务状态。
systemctl status nginx.service
由输出的状态日志(即下图画黄色框的部分)可知,80端口被占用导致启动失败
4.1.3.1 查看80端口被哪些程序所占用,输入如下命令
netstat -nap | grep 80
4.1.3.2 杀掉被占用的端口进程号,再次查看80端口是否有被占用,输入如下命令
kill -9 pid//pid为具体端口号
4.1.3.3 重启nginx服务,报错消失,再查看nginx服务状态显示successful。
systemctl start nginx
systemctl status nginx.service
4.1.4 浏览查看效果
执行命令,curl后面加上阿里云的ECS公网地址,若出现如下界面,则安装成功。
4.1.5 修改配置文件
4.1.5.1 切换至nginx服务的默认配置文件目录
cd /etc/nginx/conf.d
4.1.5.2 修改default.conf文件
vim default.conf
修改配置,项目指向到自己的自定义项目目录下
4.1.5 自定义项目网页
4.1.5.1 在刚刚修改的目录root/web下创建自定义html文件
mkdir /root/web
cd /root/web
vim index.html
4.1.5.2 重启服务
service nginx restart
(1)运行之后结果出现403报错
(2)查看root权限
ll
(3)修改文件权限
chmod 777 /root
4.1.5.3 再次重启服务,运行命令成功访问界面。
curl 139.224.232.155
4.2 Apache安装和部署
4.2.1 安装Apache
4.2.1.1下载Apache
yum install httpd -y
4.2.1.2 安装 Apache HTTP Server 的手册文档
yum install httpd-manual
4.2.1.3 启动Apache HTTP服务器
systemctl start httpd
systemctl enable httpd
4.2.2 在防火墙中永久性地添加HTTP服务的规则
4.2.2.1 启动防火墙服务
sudo systemctl start firewalld
sudo systemctl enable firewalld
4.2.2.2 设置火墙永久允许http通过
firewall-cmd --permanent --add-service=http
4.2.2.3 使用以下命令来使规则生效:
firewall-cmd --reload
4.2.2.4 修改系统防火墙配置文件,在第五行配置中增加允许80端口监听外来ip
iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -j ACCEPT
4.2.2.5 查看配置文件的内容
iptables --line -vnL
4.2.3 浏览查看效果
执行命令,curl后面加上阿里云的ECS公网地址,若出现如下界面,则安装成功。
或者在浏览器中输入阿里云ECS公网地址,若出现如下界面,则安装启动成功。
4.2.4 修改配置文件
将DocumentRoot参数修改为/home/wwwroot,再把/var/www修改为/home/wwwroot
vim /etc/httpd/conf/httpd.conf
修改配置,项目指向到自己的自定义项目目录下
4.2.5 自定义项目网页
4.2.5.1 在刚刚修改的目录root/web下创建自定义html文件
mkdir /home/wwwroot
echo "Hello World"> /home/wwwroot/index.html
4.2.5.2 重启服务
systemctl restart httpd
4.2.5.3 运行命令,成功访问界面
curl 139.224.26.160
PS:在本次实验中我只用nginx和apache部署了静态界面,动态界面的部署遇到了困难,请大佬救救。