搭建 LNMP 环境-云服务器玩法在线实验

[复制链接]
查看: 1089|回复: 0
发表于 2020-4-8 23:14:05 | 显示全部楼层 |阅读模式
实验内容
LNMP是建立web应用的平台,是Linux、NGINX,MySQL(有时也指MariaDB,数据库软件) 和PHP(有时也是指Perl或Python) 的简称。

免费在线实验地址:点击进入
实验资源:云服务器,没有云服务器的朋友推荐1折抢购:69元/年的阿里云服务器、或者99元/年的腾讯云服务器

软件环境:CentOS 6.8 64 位



一、搭建 Nginx 静态服务器
安装 Nginx

使用 yum 安装 Nginx:
  1. yum install nginx -y
复制代码
修改 /etc/nginx/conf.d/default.conf,去除对 IPv6 地址的监听,可参考下面的代码示例:
示例代码:/etc/nginx/conf.d/default.conf
  1. server {
  2.     listen       80 default_server;
  3.     # listen       [::]:80 default_server;
  4.     server_name  _;
  5.     root         /usr/share/nginx/html;

  6.     # Load configuration files for the default server block.
  7.     include /etc/nginx/default.d/*.conf;

  8.     location / {
  9.     }

  10.     error_page 404 /404.html;
  11.         location = /40x.html {
  12.     }

  13.     error_page 500 502 503 504 /50x.html;
  14.         location = /50x.html {
  15.     }

  16. }
复制代码
修改完成后,启动 Nginx:
  1. nginx
复制代码
此时,可访问实验机器外网 HTTP 服务(http://<您的 CVM IP 地址>)来确认是否已经安装成功。

将 Nginx 设置为开机自动启动:
  1. chkconfig nginx on
复制代码
CentOS 6 不支持 IPv6,需要取消对 IPv6 地址的监听,否则 Nginx 不能成功启动。

二、安装 MySQL 数据库服务
安装 MySQL
使用 yum 安装 MySQL:
  1. yum install mysql-server -y
复制代码
安装完成后,启动 MySQL 服务:
  1. service mysqld restart
复制代码
设置 MySQL 账户 root 密码:
  1. /usr/bin/mysqladmin -u root password 'Password'
复制代码
将 MySQL 设置为开机自动启动:
  1. chkconfig mysqld on
复制代码
下面命令中的密码是教程为您自动生成的,为了方便实验的进行,不建议使用其它密码。如果设置其它密码,请把密码记住,在后续的步骤会使用到。
三、搭建 PHP 环境
安装 PHP
使用 yum 安装 PHP:
  1. yum install php php-fpm php-mysql -y
复制代码
安装之后,启动 PHP-FPM 进程:
  1. service php-fpm start
复制代码
启动之后,可以使用下面的命令查看 PHP-FPM 进程监听哪个端口
  1. netstat -nlpt | grep php-fpm
复制代码
把 PHP-FPM 也设置成开机自动启动:
  1. chkconfig php-fpm on
复制代码
CentOS 6 默认已经安装了 PHP-FPM 及 PHP-MYSQL,下面命令执行的可能会提示已经安装。
PHP-FPM 默认监听 9000 端口

四、配置 Nginx 并运行 PHP 程序
配置 Nginx
在 /etc/nginx/conf.d 目录中新建一个名为 php.conf 的文件,并配置 Nginx 端口 ,配置示例如下:

示例代码:/etc/nginx/conf.d/php.conf
  1. server {
  2.     listen 8000;
  3.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  4.     location ~ .php$ {
  5.         root           /usr/share/php;
  6.         fastcgi_pass   127.0.0.1:9000;
  7.         fastcgi_index  index.php;
  8.         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  9.         include        fastcgi_params;
  10.     }
  11. }
复制代码
修改配置完成后,重启 nginx 服务
  1. service nginx restart
复制代码
这时候,我们就可以在/usr/share/php 目录下新建一个 info.php 文件来检查 php 是否安装成功了,文件内容参考如下:

示例代码:/usr/share/php/info.php
  1. <?php phpinfo(); ?>
复制代码
此时,访问 http://<您的 CVM IP 地址>:8000/info.php 可浏览到我们刚刚创建的 info.php 页面了

完成实验
恭喜!您已经成功完成了搭建 LNMP 服务器的实验任务。
腾讯云
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

精彩图文



在线客服(工作时间:9:00-22:00)
400-600-6565

内容导航

微信客服

Copyright   ©2015-2019  云服务器社区  Powered by©Discuz!  技术支持:尊托网络     ( 湘ICP备15009499号-1 )