Centos7.2 / Linux的下搭建LAMP環境的搭建和配置虛擬主機

LAMP環境:LINUX + Apahce的+ MYSQL + PHP的網絡服務器環境。

(一)。準備工作

1.下載並安裝CentOS7.2,配置好網絡環境,確保centos能上網,可以獲取到yum源(阿里雲服務器已配置好,不需要自己動手)
2。配置防火牆,開啓21,80,3306端口。 CentOS 7.0默認使用的是防火牆作爲防火牆,這裏改爲iptables 防火牆停止防火牆服務輸入命令systemctl stop firewalld.service 禁止防火牆開機啓動輸入命令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啓用iptables.service















  1. 關閉seinux(阿里雲已配置好)
    修改配置文件
    vi / etc / selinux / config

SELINUX =強制#註釋掉
SELINUXTYPE =有針對性#註釋掉
SELINUX =禁用#增加
:wq!#退出保存
使配置|立即生效

setenforce 0

(二)。安裝燈環境
1.安裝apahce (http://www.1jaz.com
yum install httpd 
設置apache開機啓動
systemctl啓用httpd.service 
重啓apache服務系統
重啓httpd.service

ps:可能會用到的:
systemctl start httpd.service#啓動apache 
systemctl stop httpd.service #dail止apache 
systemctl restart httpd.service#重啓apache 
systemctl enable httpd.service#settings置apache開機啓動

2.安裝mysql(可不安裝)
由於yum源上沒有mysql-server。所以必須去官網下載,這裏我們用wget命令,直接獲取。依次輸入下方三條命令:    
wget http://dev.mysql.com/get /mysql-community-release-el7-5.noarch.rpm    http://www.1jaz.com
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>爲'root'設置密碼@'localhost'=密碼('123456');

遠程連接設置,所有以root賬號連接的遠程用戶,設其密碼爲123456 
mysql>授予所有權限來自'123456'標識的@'%';

更新權限
mysql>刷新權限;

退出,mysql 
退回回車

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重啓mysqld.service 
systemctl restart httpd.service

(三。)配置虛擬主機
1.創建自定義網站根目錄
mkdir / home / webroot 
2.打開httpd主配置文件
vim /etc/httpd/conf/httpd.conf 
3.找到以下代碼


AllowOverride無

# Allow open access:Require all granted


將其改爲:


AllowOverride All

# Allow open access:Require all granted


4.找到以下代碼


# 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


改爲[Options -Indexes爲禁止訪問目錄列表]:


# 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 FollowSymLinksOptions -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


5.追加自動壓縮網頁代碼功能,在主配置文件繼續加入
添加Gzip


SetOutputFilter DEFLATE 
SetEnvIfNoCase Request_URI。(?:gif | jpe?g | png)$ no-gzip不變
SetEnvIfNoCase Request_URI。(?:exe | t?gz | zip | bz2 | sit | rar)$ no-gzip不變
SetEnvIfNoCase Request_URI。(?:pdf | mov | avi | mp3 | mp4 | rm)$ no-gzip不要改變
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

6.建立一個項目目錄
mkdir / home / webroot / demo 
7.進入apache子配置文件夾,建立對應的項目配置文件
cd /etc/httpd/conf.d 
vim demo.conf 
8.在打開的配置文件中,輸入以下代碼


DocumentRoot“/ home / webroot / demo” 
ServerName你的公網IP

ps:如果已經申請下域名,則輸入以下代碼(上方作者),允許r-collar.com www.hehaoke.com兩種方式的域名訪問,並禁止直接訪問服務器IP的方式訪問項目

ServerName你的公網IP

訂單允許,拒絕
所有人拒絕



DocumentRoot“/ home / webroot / demo” 
ServerName r-collar.com

DocumentRoot“/ home / webroot / demo” 
ServerName www.hehaoke.com

9.重啓apache 
systemctl重啓httpd.service

注:此方式是基於端口實現的虛擬主機,如要添加新端口來訪問新項目,還需要注意去主配置文件httpd.conf中給新端口添加監聽,並在防火牆中允許新端口的訪問。

更多:http://www.1jaz.com


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章