LAMP搭建總結(jié)
LAMP 搭建總結(jié)搭建思路如圖:安裝順序:L(Linux)操作系統(tǒng)----->A(Apache)網(wǎng)站服務(wù)軟件------->M(mysql)數(shù)據(jù)庫軟件------->P(php )php 解析軟件一、
LAMP 搭建總結(jié)
搭建思路如圖:
安裝順序:L(Linux)操作系統(tǒng)----->A(Apache)網(wǎng)站服務(wù)軟件------->M(mysql)數(shù)據(jù)庫軟件------->P(php )php 解析軟件
一、Linux
(一)選擇Linux 發(fā)行版,我們選擇centos6.6的64位操作系統(tǒng)來進行安裝。選擇minial 最小化安裝。
(二)Ip 地址配置、ssh 配置、selinux 關(guān)閉、iptables 防火墻配置 ip 地址配置
vi /etc/sysconfig/network-scripts/ifcfg-eth0,設(shè)置
ONBOOT=yes ------>網(wǎng)卡開機啟動
BOOTPROTO=static -------->網(wǎng)上的模式為(static)靜態(tài)

IP
,IPADDR=192.168.1.252 ----->ip地址
NETMASK=255.255.255.0 ------->子網(wǎng)掩碼
GATEWAY=192.168.1.1 ------->網(wǎng)關(guān)
DNS1=202.103.224.68 -------->第一個DNS 服務(wù)器
DNS2=202.103.225.68 -------->第二個DNS 服務(wù)器
Esc ,然后:wq保存退出。
ssh(openssh)配置
對于ssh ,我們一般要更改其端口號,禁止root 用戶遠程登陸,與配置rsa 秘鑰對連接。
修改端口號,與禁止root 用戶登陸:
vim /etc/ssh/sshd_config
找到:
#Port 22 ---->去掉前面的井號,并把22改成你想要的端口號
#PermitRootLogin yes -------->去掉前面的井號,并把yes 改成no 禁止root 登陸。
使用rsa 秘鑰對:
使用puttygen 生成rsa 秘鑰對,保存私鑰到本機,在登陸的用戶家目錄下面新建.ssh/authorized_keys文件
mkdir /root/.ssh
vi /root/.ssh/authorized_keys
然后把公鑰的文本復(fù)制進authorized_keys文件里面保存退出。
最后設(shè)置對應(yīng)的權(quán)限:
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
關(guān)閉selinux
,setenforce0
vi /etc/selinux/config
把SELINUX=enforcing改成SELINUX=disabled
配置iptables 防火墻
清空filter 表的所有規(guī)則
iptables -t filter -F
iptables -t filter -X
iptables -t filter -Z
!?。。。ㄗ⒁猓┨砑觭sh 的端口進filter 表里的input
鏈,防止操作引起把自己封在外面。!?。?!
Iptables -t filter -I INUPT -p tcp -m tcp --dport 22 -j ACCEPT (22可以改成你設(shè)置的ssh 端口)
把filter 表里的三個默認鏈分別設(shè)置成:
iptables -P INPUT DORP ---->入站先全部禁止掉
Iptables -P FORWORD ACCEPT ----->轉(zhuǎn)發(fā)打開
Iptables -P OUTPUT ACCEPT ----->出站打開,免除多操作
除去上面開放的ssh 端口外,還有要開放的端口有:
Io 端口:iptables -A INPUT -i lo -j ACCEPT -->主機環(huán)回端口開啟,以免有不必要的麻煩
ICMP 端口:iptables -A INPUT -i eth0 -p icmp --icmp-type 8 -j ACCEPT Ping 檢測口,在網(wǎng)絡(luò)排查時必要的接口。
DNS 服務(wù)端口:iptables -A INPUT -i eth0 -p udp --sport 53 -j ACCEPT 域名解析服務(wù)器,不打開無法ping 通baidu.com 域名,也用不了yum 工具。
Apache (80)端口:iptables -I INPUT -p tcp --dport 80 -j ACCEPT Httpd 的端口,網(wǎng)站服務(wù)最主要用的。
(三)Yum 安裝Lamp 一些基本的調(diào)用庫
Apach Mysql所需要的庫:
yum install -y libaio library cmake glibc gcc zlib-devel pcre pcre-devel
,php 所需要的庫:
需要源:epel (libmcrypt-devel這個包)
yum install -y epel-release
(i386)
Rpm -Uvh http://mirrors.ustc.edu.cn/epel/6/i386/epel-release-6-8.noarch.rpm
(x86_64)
Rpm -Uvh http://mirrors.ustc.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install -y libxml2-devel openssl openssl-devel bzip2 bzip2-devel libpng libpng-devel freetype-devel libmcrypt-devel libjpeg-devel
二、Apache
(一)下載,解壓,編譯安裝Apache
a. Apache2.2版本
wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.2.31.tar.bz2 tar -jxvf httpd-2.2.31.tar.bz2
cd httpd-2.2.31
./configure
--prefix=/usr/local/apache2
--with-included-apr
--enable-so
--enable-deflate=shared
--enable-expires=shared
--enable-rewrite=shared
--with-pcre
make
make install
為了能夠正確配置,應(yīng)該在每一步都進行echo $?來檢查 是否為0。為0則表示上一個動作已經(jīng)成功。
b. Apache 2.4 版本
因為2.4把apr 分離出來了,所以要先下載然后編譯安裝好才安裝Apache 。 wget http://mirrors.hust.edu.cn/apache/apr/apr-1.5.2.tar.bz2
wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.5.4.tar.bz2
,tar -jxvf apr-1.5.2.tar.bz2
tar -jxvf apr-util-1.5.4.tar.bz2
安裝apr ./configure --prefix=/usr/local/apr
注意:在這一步很容易出錯,編譯之后會提示 rm: cannot remove `libtoolT': No such file or directory ,在出現(xiàn)這個提示之后如果使用命令 echo $? 來查看是否編譯成功時,會顯示0(成功),但
是時候到編譯apache 的時候會重復(fù)出錯,而原因正是因為編譯apr 時的這個錯誤。
解決方法:在 apr-1.5.2/ 目錄下打開configure 文件,找到 “ $RM "$cfgfile" ” ,將這一行注釋掉即
可,時候重新編譯。
make&&make install
安裝apr-util
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
make&&make install httpd 安裝
./configure
--prefix=/usr/local/apache2
--enable-so
--enable-deflate=shared
--enable-expires=shared
--enable-rewrite=shared
--with-pcre --with-apr=/usr/local/apr
--with-apr-util=/usr/local/apr-util/
Make
make install (二)設(shè)置Apache 的主配置文件與虛擬主機配置文件
a. 主配置文件的設(shè)置:
vim /usr/local/apache2/conf/httpd.conf
Apache2.2找到
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
修改為
Options FollowSymLinks
,AllowOverride None
Order deny,allow
Allow from all
◆ Apache2.4找到
AllowOverride none
Require all denied
修改為
AllowOverride none
Require all granted
◆ 找到Include conf/extra/httpd-vhosts.conf把前面的井號去掉,開啟虛擬主機。
b. 虛擬主機配置文件
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
# ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/tmp" ---->打開這里,tmp 改成你的網(wǎng)站目錄 ServerName dummy-host.example.com ---->這里寫你的域名 # ServerAlias www.dummy-host.example.com
# ErrorLog "logs/dummy-host.example.com-error_log"
# CustomLog "logs/dummy-host.example.com-access_log" common
(三)設(shè)置301跳轉(zhuǎn)、防盜鏈、靜態(tài)頁面、訪問控制等。
◆ 修改默認主機
,復(fù)制黏貼vhost 主機配置,使其排在前頭,能禁掉別的域名轉(zhuǎn)向你的服務(wù)器。 主機的目錄必須存在。
◆ 301跳轉(zhuǎn)
vim /usr/local/apache2/conf/httpd.conf
找到LoadModule rewrite_module modules/mod_rewrite.so
把前面的#號去掉
添加
RewriteEngine on
RewriteCond {HTTP_HOST} ^www.vm.cn [OR] --->跳轉(zhuǎn)前輸入的域名
RewriteCond {HTTP_HOST} ^www.vm2.cn$--->跳轉(zhuǎn)前輸入的域名 RewriteRule ^/(.*)$ http://www.avm.cn/$1 [R=301,L]--->跳轉(zhuǎn)后
◆ 用戶認證
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
添加
AllowOverride AuthConfig
AuthName "server"
AuthType Basic
AuthUserFile /www/.htpasswd
require valid-user
創(chuàng)建用戶
/usr/local/apache2/bin/htpasswd -c /www/.htpasswd admin
◆ 防盜鏈
SetEnvIfNoCase Referer "^http://.*.vm.cn" local_ref #白名單
SetEnvIfNoCase Referer ".*.vm.cn" local_ref #白名單
Order Allow,Deny
Allow from env=local_ref
,◆ 靜態(tài)頁面
在httpd.conf 主文件里找到
LoadModule expires_module modules/mod_expires.so
刪掉前面的#號啟動模塊
然后在/usr/local/apache2/conf/extra/httpd-vhosts.conf
加入
ExpiresActive on
ExpiresByType image/gif "access plus 1 days"
ExpiresByType image/jpeg "access plus 24 hours" ExpiresByType image/png "access plus 24 hours" ExpiresByType text/css "now plus 2 hour"
ExpiresByType application/x-javascript "now plus 2 hours" ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min"
◆ 訪問控制 Apache2.2
網(wǎng)站禁止
Order allow,deny #deny(禁止) 在前面,就先看這個語句。allow (允許)一般寫在前面
Allow from all
Deny from 192.168.1.22 #禁止192.168.1.22訪問
禁止訪問某些文件
Order deny,allow
Deny from all
Allow from 192.168.1.210 #允許192.168.1.210訪問
Apache2.4
禁止ip 訪問網(wǎng)站
AllowOverride none
Require all granted
,Require not ip 192.168.1.210
禁止訪問某些文件
Require all denied
Require ip 192.168.1.251
◆ 限制user_agent
在配置文件找到
內(nèi)加入
Rewritecond {HTTP_USER_AGENT} ^.*curl.* [NC,OR] Rewritecond {HTTP_USER_AGENT} ^.*chrome.* [NC] Rewriterule .* - [F]
能夠限制某些瀏覽器類型。
(四)配置記錄日志
◆ 日志按每天進行切割。
先把vhost 配置下面的
common
換成
combined
然后在“”中加入
"|/usr/local/apache2/bin/rotatelogs -l
/usr/local/apache2/logs/hd.com-access_Ymd_log 86400" 以每天進行分割。
◆ 自定義記錄
在customlog 行上頭加入
setEnvIf Request_URI ".*.gif$" image-request setEnvIf Request_URI ".*.jpg$" image-request setEnvIf Request_URI ".*.png$" image-request setEnvIf Request_URI ".*.bmp$" image-request setEnvIf Request_URI ".*.swf$" image-request setEnvIf Request_URI ".*.js$" image-request
setEnvIf Request_URI ".*.css$" image-request
然后在customlog 行這行的末尾加 上 env=!image-request
,三、Mysql
(一)下載并編譯安裝Mysql
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
tar -zxvf mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz mv mysql-5.7.10-linux-glibc2.5-x86_64 /usr/local/mysql useradd -s /sbin/nologin -M mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
5.5以下
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
5.7以上
./bin/mysqld --user=mysql --datadir=/data/mysql --initialize ./bin/mysql_ssl_rsa_setup --datadir=/data/mysql
(二)加入init.d 服務(wù),并啟動mysql
cp support-files/my-default.cnf /etc/my.cnf vim /etc/my.cnf //編輯或者修改 basedir = /usr/local/mysql datadir = /data/mysql
port = 3306
socket = /tmp/mysql.sock
cp support-files/mysql.server /etc/init.d/mysqld chmod 755 /etc/init.d/mysqld
vim /etc/init.d/mysqld
修改basedir=/usr/local/mysql
datadir=/data/mysql
chkconfig --add mysqld
chkconfig mysqld on
service mysqld restart
ps aux |grep mysqld
(三)修改默認數(shù)據(jù)庫密碼
5.5以下沒有初始密碼的