Centos7.2LAMP环境的搭建&配置虚拟主机

时间:2022-10-24 16:23:09
(一).准备工作
1.下载并安装CentOS7.2,配置好网络环境,确保centos能上网,可以获取到yum源(阿里云服务器已配置好,不需要自己动手)
2.配置防火墙,开启21、80、3306端口。CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。
#停止firewall服务 输入命令
systemctl stop firewalld.service
#禁止firewall开机启动 输入命令
systemctl disable firewalld.service
#安装iptables防火墙 输入命令:
yum install iptables-services
#编辑防火墙配置文件 输入命令
vim /etc/sysconfig/iptables
#打开后,在 -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 下方添加:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
#保存退出
#最后重启防火墙使配置生效
systemctl restart iptables.service
#设置防火墙开机启动
systemctl enable iptables.service
3. 关闭seinux(阿里云已配置好)
#修改配置文件
vi /etc/selinux/config
 
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
#使配置立即生效
setenforce 0 
(二).安装Lamp环境
1.安装apahce
yum install httpd
设置apache开机启动
systemctl enable httpd.service
重启apache服务
systemctl restart httpd.service


ps:可能会用到的:
systemctl start httpd.service #启动apache
systemctl stop httpd.service #停止apache
systemctl restart httpd.service #重启apache
systemctl enable httpd.service #设置apache开机启动


2.安装mysql(可不安装)
#由于yum源上没有mysql-server。所以必须去官网下载,这里 我们用wget命令,直接获取。依次输入下方三条命令:    
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
        
#安装完成后重启mysql,输入命令:
systemctl restart mysqld.service


#此时mysql的root用户没有密码:    
[root@linuxidc-web linuxidc]# mysql -u root 


#设置msyql密码为 123456
mysql> set password for 'root'@'localhost' =password('123456');


#远程连接设置,所有以root账号连接的远程用户,设其密码为 123456
mysql> grant all privileges on *.* to root@'%'identified by '123456';


#更新权限
mysql>flush privileges; 


#退出,mysql
exit 回车


3.安装php 输入命令:
yum install php
#安装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
#重启对应服务
systemctl restart mysqld.service
systemctl restart httpd.service


(三.)配置虚拟主机
1.创建自定义网站根目录
mkdir /home/webroot
2.打开httpd主配置文件
vim /etc/httpd/conf/httpd.conf
3.找到以下代码
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
将其改为:
<Directory "/home/webroot">
    AllowOverride All
    # Allow open access:
    Require all granted
</Directory>
4.找到以下代码
<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks


    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None


    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>
改为[Options -Indexes为禁止访问目录列表]:
<Directory "/home/webroot">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks
    Options -Indexes
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None


    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>
5.追加自动压缩网页代码功能,在主配置文件继续加入
# Gzip
<IfModule deflate_module>
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
    AddOutputFilterByType DEFLATE text/*
    AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript appliction/x-javascript
    AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp
</IfModule>
6.建立一个项目目录
mkdir /home/webroot/demo
7.进入apache子配置文件夹,建立对应的项目配置文件
cd /etc/httpd/conf.d
vim demo.conf
8.在打开的配置文件中,输入以下代码
<VirtualHost *:80>
    DocumentRoot "/home/webroot/demo"
    ServerName 你的公网IP
</VirtualHost>
ps:如果已经申请下域名,则输入以下代码(上方作废),允许r-collar.com www.r-collar.com两种方式的域名访问,并禁止直接访问服务器IP的方式访问项目
<VirtualHost *:80>
    ServerName 你的公网IP
    <Location />
        Order Allow,Deny
        Deny from all
   </Location>
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/home/webroot/demo"
    ServerName r-collar.com
</VirtualHost>


<VirtualHost *:80>
    DocumentRoot "/home/webroot/demo"
    ServerName www.r-collar.com
</VirtualHost>


9.重启apache
systemctl restart httpd.service


注:此方式是基于端口实现的虚拟主机,如要添加新端口来访问新项目,还需要注意去主配置文件httpd.conf给新端口添加监听,并在防火墙中允许新端口的访问.