美文网首页
LAMP搭建笔记

LAMP搭建笔记

作者: geekdeedy | 来源:发表于2017-08-07 21:51 被阅读0次

    LAMP简介

    LAMP指的Linux(操作系统)、ApacheHTTP 服务器,MySQL(有时也指MariaDB,数据库软件) 和PHP(有时也是指Perl或Python) 的第一个字母。LAMP便成了一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。

    安装步骤:

    安装服务
    配置服务
    启动服务
    测试服务
    

    安装服务

    CentOS 6:

    yum install httpd php mysql-server php-mysql 
    

    CentOS 7:

    yum install httpd php php-mysql mariadb-server mariadb
    

    (php-mysql用于支持php连接mysql的组件)

    配置服务

    配置httpd

    Apache的主配置文件:/etc/httpd/conf/httpd.conf
    主配置文件一般不去修改,其中有几个选项可以自定义修改。
    DocumentRoot /var/www/html/ //指定默认主页路径
    Listen 80 //httpd的监听端口,可以设置为监听指定网卡"Listen [IP:]PORT",如果需要修改端口,还要同时修改防火墙和selinux的策略

    <IfModule dir_module>
        DirectoryIndex index.html
    </IfModule>
    

    DirecotryIndex index.html //默认主页,如果有多个,优先级为由左至右
    默认站点主目录:/var/www/html/
    启动Apache服务后,Apache服务会到默认主目录查找默认主页文件

    配置mysql

    mysql和mariadb数据库服务软件默认提供一个安全脚本,执行这个脚本,可以对mysql进行基本的安全配置,比如设置密码、删除匿名用户等

    mysql_secure_installation //执行MYSQL数据库安全脚本
    
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
          SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
    
    In order to log into MariaDB to secure it, we'll need the current
    password for the root user.  If you've just installed MariaDB, and
    you haven't set the root password yet, the password will be blank,
    so you should just press enter here.
    
    Enter current password for root (enter for none):  
    //验证输入数据库管理用户ROOT的密码,默认为空,回车即可
    OK, successfully used password, moving on...
    
    Setting the root password ensures that nobody can log into the MariaDB
    root user without the proper authorisation.
    
    You already have a root password set, so you can safely answer 'n'.
    
    Change the root password? [Y/n] y
    //是否更改数据库管理员ROOT用户的密码,如果选Y则需要重复输入两遍新口令,N跳过
    New password: 
    Re-enter new password: 
    Password updated successfully!
    Reloading privilege tables..
     ... Success!
    
    
    By default, a MariaDB installation has an anonymous user, allowing anyone
    to log into MariaDB without having to have a user account created for
    them.  This is intended only for testing, and to make the installation
    go a bit smoother.  You should remove them before moving into a
    production environment.
    
    Remove anonymous users? [Y/n] y
    //是否移除匿名用户,即不输入用户名和密码即可访问,建议移除(Y移除,N不移除)
     ... Success!
    
    Normally, root should only be allowed to connect from 'localhost'.  This
    ensures that someone cannot guess at the root password from the network.
    
    Disallow root login remotely? [Y/n] n
    //是否禁用root用户远程登录(Y禁止,N允许)
     ... skipping.
    
    By default, MariaDB comes with a database named 'test' that anyone can
    access.  This is also intended only for testing, and should be removed
    before moving into a production environment.
    
    Remove test database and access to it? [Y/n] n
     //是否删除数据库软件自动生成的测试数据库(Y删除,N不删除)
     ... skipping.
    
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    
    Reload privilege tables now? [Y/n] y 
    //是否立即重载权限表(Y重载,N不重载)
     ... Success!
    
    Cleaning up...
    
    All done!  If you've completed all of the above steps, your MariaDB
    installation should now be secure.
    
    Thanks for using MariaDB!
    

    启动服务

    CentOS 6:
    service httpd start //启动httpd服务
    service mysqld start //启动mysql数据库服务
    CentOS 7:
    systemctl start httpd //启动httpd服务
    systemctl start mariadb //启动mariadb数据库服务

    测试服务

    测试WEB运行环境

    echo "WEB服务器运行环境良好" >> /var/www/html/index.html
    

    使用浏览器或者curl、link等软件访问http://127.0.0.1/index.html,如果页面显示php环境运行良好,即PHP运行环境可以正常使用,否则不能正常使用。

    测试php运行环境

    echo "<?php echo "PHP环境运行良好"; ?>" >> /var/www/html/index.php
    

    使用浏览器或者curl、link等软件访问http://127.0.0.1/index.php,如果页面显示php环境运行良好,即PHP运行环境可以正常使用,否则不能正常使用。

    测试运行连通性

    cat << EOF >/var/www/html/mysql.php
    <?php
    $mysqli=new mysqli("localhost","root","00000000"); 
    if(mysqli_connect_errno()){
    echo " 连接数据库失败:";
    $mysqli=null;
    exit;
    }
    echo " 连接数据库成功!";
    $mysqli->close();
    ?>
    EOF
    

    //$mysqli=new mysqli("localhost","root","00000000");
    //localhost为服务器地址,root为mysql用户,00000000为mysql用户的密码。
    使用浏览器或者curl、link等软件访问http://127.0.0.1/mysql.php,如果页面显示连接数据库成功!即PHP运行环境可以正常使用且数据库也可以正常访问,否则不能正常使用。
    最后编辑httpd文件让httpd服务识别.php和.phps文件
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    <IfModule dir_module>
    DirectoryIndex index.php index.html
    </IfModule>
    至此LAMP环境搭建完成!

    相关文章

      网友评论

          本文标题:LAMP搭建笔记

          本文链接:https://www.haomeiwen.com/subject/hlczlxtx.html