Centos7.2LAMP環境的搭建&配置虛擬主機

(一).準備工作
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給新端口添加監聽,並在防火牆中允許新端口的訪問.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章