美文网首页LinuxLinux 相关文章Linux运维
CentOS 7中 LAMP服务器搭建及配置

CentOS 7中 LAMP服务器搭建及配置

作者: caoleic | 来源:发表于2016-11-23 23:24 被阅读655次
    • 系统:CentOS 7
    • 搭建 LAMP(Linux+Apache+Mysql+PHP) 服务器

    初始设置

    查看 linux 系统版本

    # cat /etc/redhat-release
    

    更新系统

    # yum update
    # yum upgrade
    

    检查当前系统是否已安装 apache 和 mysql

    # yum list installed | grep httpd
    # rpm -qa | grep httpd
    # yum list installed | grep mysql
    # rpm -qa | grep mysql
    

    如果已经存在,请先执行卸载命令,不同的安装方式使用不同的卸载方式,例:

    # yum remove httpd
    

    # rpm -e httpd
    

    # make uninstall httpd
    

    添加apache和mysql用户

    # useradd -s /sbin/nologin -M apache
    # useradd -s /sbin/nologin -M mysql
    

    CentOS 7.0 默认使用的是firewall作为防火墙,修改为iptables防火墙

    1. 关闭firewall
      停止firewall
    # systemctl stop firewalld.service
    

    禁止firewall开机启动

    # systemctl disable firewalld.service
    
    1. 安装iptables防火墙
    # yum install iptables-services
    

    编辑防火墙配置文件

    # vi /etc/sysconfig/iptables
    

    编辑 iptables 文件内容

    # Firewall configuration written by system-config-firewall
    # Manual customization of this file is not recommended.
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    -A INPUT -p icmp -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited
    COMMIT
    

    新增内容

    -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
    

    重启防火墙使配置生效

    # systemctl restart iptables.service
    

    设置防火墙开机启动

    # systemctl enable iptables.service
    

    关闭SELINUX

    # vi /etc/selinux/config
    

    编辑 config 文件内容

    #SELINUX=enforcing
    #SELINUXTYPE=targeted
    SELINUX=disabled
    

    使配置立即生效

    # setenforce 0
    

    开始安装

    安装软件时,根据提示,输入Y安装即可成功安装

    1. 安装Apache
    # yum install httpd
    

    启动apache

    # systemctl start httpd.service
    

    停止apache

    # systemctl stop httpd.service
    

    重启apache

    # systemctl restart httpd.service
    

    设置apache开机启动

    # systemctl enable httpd.service
    

    在客户端浏览器中打开服务器IP地址,会出现apache默认界面,即apache安装成功

    1. 安装mysql
      CentOS 7的yum源中没有mysql,需要下载mysql的repo源
    • 下载mysql的repo源
    # wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
    
    • 安装mysql-community-release-el7-5.noarch.rpm包
    # rpm -ivh mysql-community-release-el7-5.noarch.rpm
    

    安装这个包后,会获得两个mysql的yum repo源:

    # /etc/yum.repos.d/mysql-community.repo
    # /etc/yum.repos.d/mysql-community-source.repo
    
    • 安装mysql
    # yum install mysql-server
    

    根据步骤安装,到目前为止还没有设置数据库的密码

    • 重置密码
      重置密码前,首先要登录
    # mysql -u root
    

    登录时有可能报这样的错误:ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2),原因是/var/lib/mysql的访问权限问题。

    # 
    

    重启 mysql 服务

    # service mysqld restart
    

    重置密码

    # mysql -u root
    
    mysql > use mysql;
    mysql > update user set password=password('123456') where user='root';
    mysql > exit;
    
    1. 安装PHP
    # yum install php
    
    1. 安装PHP组件,使PHP支持 mysql
    # yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
    

    配置环境

    1. Apache配置
      编辑 httpd.conf 文件内容
    # vi /etc/httpd/conf/httpd.conf
    
    //添加ServerSignature On (在错误页中显示Apache的版本) 
    ServerSignature On
    //允许服务器执行CGI及SSI,禁止列出目录
    Options Indexes FollowSymLinks #修改为:Options Includes ExecCGI FollowSymLinks
    //允许.htaccess
    AllowOverride None #修改为:AllowOverride All
    //设置不在浏览器上显示树状目录结构
    #Options Indexes FollowSymLinks #修改为 Options FollowSymLinks
    //设置默认首页文件,增加index.php
    DirectoryIndex index.html#修改为:DirectoryIndex index.html index.htm index.php
    //添加MaxKeepAliveRequests 500 (增加同时连接数)
    MaxKeepAliveRequests 500
    

    删除默认测试页

    # rm -f /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html
    
    1. php配置
      编辑 php.ini 文件内容
    # vi /etc/php.ini
    
    //设置时区,把前面的分号去掉
    date.timezone = PRC
    //禁止显示php版本的信息
    expose_php = Off
    //支持php短标签
    short_open_tag = ON
    

    重启apache

    # systemctl restart httpd.service
    

    测试服务

    # cd /var/www/html
    
    # vi index.php
    

    输入下面内容

    <?php
    phpinfo();
    ?>
    

    注:apache 默认的程序目录是/var/www/html
    权限设置:chown apache.apache -R /var/www/html
    权限设置:chown mysql.mysql -R /var/lib/mysql


    相关文章

      网友评论

        本文标题:CentOS 7中 LAMP服务器搭建及配置

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