今天来记录一下VPS centos7 搭建多个站点详细完整

来源:互联网 发布:淘宝手机优惠券尺寸 编辑:程序博客网 时间:2024/06/01 07:20

首先搭建LAMP环境

LAMP是Linux+Apache+MySQL+PHP的简称,用于搭建web服务器

1)安装Apache软件

要安装Apache软件,用下面的命令:

[root@localhost ~]# yum install httpd

安装完成后, 启动Apache并将其设置为开机启动

[root@localhost ~]# systemctl start httpd.service[root@localhost ~]# systemctl enable httpd.service

检查httpd服务是否开启成功

[root@localhost ~]# systemctl status httpd.service

出现“ enabled”表示httpd服务已设为开机启动,
“ active(running)表示httpd服务正在运行中


由于centos7默认开启了防火墙,HTTP使用端口80,因此防火墙要开放80端口

[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent

更改防火墙后需要重启才能生效

[root@localhost ~]# firewall-cmd --reload

检查防火墙配置是否成功:

[root@localhost ~]# firewall-cmd --list-all

OK!一切顺利的话,在浏览器输入VPS的IP就可以访问Apache自带的测试页面了。


Apache软件的主配置文件为/etc/httpd/conf/httpd.conf
而且位于/etc/httpd/conf.d目录下以.conf结尾的配置文件都会被读取;

2)安装PHP软件

要安装PHP软件,用下面的命令:

[root@localhost ~]# yum install php

然后,重启httpd服务:

[root@localhost ~]# systemctl restart httpd

为了测试Apache能不能正常调用PHP,在/var/www/html目录下新建一个phpinfo.php 内容如下:

<?php phpinfo() ?>

phpinfo ();是PHP程序提供的一个函式库,一切正常的话浏览器会显示web服务器的相关信息。测试完后,还是把phpinfo.php这个文件删除掉

3)安装MySQL软件

网上一般都是

[root@localhost ~]# yum install mysql[root@localhost ~]# yum install mysql-devel[root@localhost ~]# yum install mysql-server

因为系统是CentOS 7 yum将MySQL数据库软件从默认的程序列表中移除。推荐使用的是MariaDB,由于对MariaDB不熟悉,现在只讲MySQL

在官网下载安装mysql-server

[root@localhost ~]# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm[root@localhost ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm[root@localhost ~]# yum install mysql-community-server

安装完成后,启动mysqld服务并将其设为开机启动:

[root@localhost ~]# systemctl start mysqld[root@localhost ~]# systemctl enable mysqld

然后,检查mysqld服务状态:

[root@localhost ~]# systemctl status mysqld

初次安装mysql,root账户没有密码。

[root@localhost~]# mysql -u root Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.6.26 MySQL Community Server (GPL)Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               |+--------------------+rows in set (0.01 sec)mysql>

设置密码

mysql> set password for 'root'@'localhost' =password('password');Query OK, 0 rows affected (0.00 sec)mysql> 

如果是新用户而不是root,则要先新建用户

mysql>create user 'username'@'%' identified by 'password';  

MySQL安装完成;

4)安装php-mysql

php-mysql是一个用于让PHP程序使用MySQL数据库的模块。使用以下命令:

[root@localhost ~]# yum install php-mysql

然后,重启httpd服务:

[root@localhost ~]# systemctl restart httpd

到此LAMP环境就搭建好了。

接下来配置Apache环境来搭建多个站点

在/etc/httpd/conf.d里面创建一个Vhost.conf文件 写入如下内容:

NameVirtualHost *:80<VirtualHost *:80>    ServerAdmin name@local.com    ServerName AAAAAAA.com    ServerAlias www.AAAAAAA.com AAAAAAA.com    DocumentRoot /var/www/html/AAAAAAA.com    ErrorLog  /var/www/html/AAAAAAA.com/logs/error.log    CustomLog /var/www/html/AAAAAAA.com/logs/access.log combined    DirectoryIndex index.html index.php</VirtualHost><Directory "/var/www/html/AAAAAAA.com">    Options FollowSymLinks    AllowOverride All</Directory><VirtualHost *:80>    ServerAdmin name@local.com    ServerName BBBBBBB.com    ServerAlias www.BBBBBBB.com BBBBBBB.com    DocumentRoot /var/www/html/BBBBBBB.com    ErrorLog  /var/www/html/BBBBBBB.com/logs/error.log    CustomLog /var/www/html/BBBBBBB.com/logs/access.log combined    DirectoryIndex index.html index.php</VirtualHost><Directory "/var/www/html/BBBBBBB.com">    Options FollowSymLinks    AllowOverride All</Directory>    

ServerAdmin 是管理员的邮箱
ServerName 虚拟主机的域名
ServerAlias 子域名映射到虚拟主机域名
DocumentRoot 网页文件存放的根目录
CustomLog 和 CustomLog 是日志文件的存放目录


Apache软件的主配置文件为/etc/httpd/conf/httpd.conf
下面是对主配置文件中URL配置的重定向

<Directory  >    Options FollowSymLinks    AllowOverride All</Directory>

记得将AAAAAAA和BBBBBBB替换成你的域名

修改完配置文件后,记得一定要重启apache,否则修改是不会起到作用的,这是很多刚上手的人员最容易忽略的问题。

[root@localhost ~]# systemctl restart httpd

就酱!,一个服务器放置多个站点的配置工作就完成了。

0 0
原创粉丝点击