LAMP
Linux + Apache(httpd) + MySQL + PHPjavascript
MySQL的安裝
wget [下載地址] 文件名相似這樣:mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
tar zxvf 解壓
mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql 移動目錄並更名
cd /usr/local/mysql 放置目錄
useradd mysql
mkdir /data/ 數據目錄
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql 初始化
vim /etc/my.cnf 編輯內容爲:
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
cp support-files/mysql.server /etc/init.d/mysqld
vi /etc/init.d/mysqld
定義basedir(/usr/local/mysql)和datadir(/data/mysql)
/etc/init.d/mysqld start
service mysqld startphp
Mariadb的安裝
cd /usr/local/src
wget [下載地址] 文件名相似mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
tar zxvf mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz 解壓
mv mariadb-10.2.6-linux-glibc_214-x86_64 /usr/local/mariadb 移動並更名
cd /usr/local/mariadb
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mariadb/ --datadir=/data/mariadb 初始化
cp support-files/my-small.cnf /usr/local/mariadb/my.cnf
cp support-files/mysql.server /etc/init.d/mariadb
vim /etc/init.d/mariadb //定義basedir、datadir、conf以及啓動參數
/etc/init.d/mariadb startcss
Apache(httpd)的安裝
wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.27.tar.gz
wget http://mirrors.hust.edu.cn/apache/apr/apr-1.5.2.tar.gz
wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.5.4.tar.gz
apr和apr-util是一個通用的函數庫,它讓httpd能夠不關心底層的操做系統平臺,能夠很方便地移植(從linux移植到windows)
解壓
cd apr-1.6.3/
./configure --prefix=/usr/local/apr!
make && make install
完成apr的安裝
cd /usr/local/src/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
完成apr-util的安裝
cd /usr/local/src/httpd-2.4.29
./configure \ (反斜槓是脫義字符,加上它能夠把一行命令寫成多行)
--prefix=/usr/local/apache2.4 \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--enable-so \
--enable-mods-shared=most
make && make install
完成Apache的安裝html
PHP5的安裝
wget http://cn2.php.net/distributions/php-5.6.30.tar.bz2
tar -jxvf php-5.6.30.tar.gz
cd php-5.6.30
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
make && make install
cp php.ini-production /usr/local/php/etc/php.inijava
PHP7的安裝
wget http://cn2.php.net/distributions/php-7.1.6.tar.bz2
tar -zxvf php-7.1.6.tar.bz2
cd php-7.1.6
./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php7/etc --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
make && make install
cp php.ini-production /usr/local/php7/etc/php.inimysql
Apache和PHP結合
vim /usr/local/apache2.4/conf/httpd.conf 編輯文件修改如下內容
#ServerName www.example.com:80 將井號去掉
LoadModule php7_module modules/libphp7.so 前面加上井號,註釋掉,由於同時安裝了php5和7,註釋其中一個
Require all denied 改成 Require all granted
AddType application/x-compress .Z AddType application/x-gzip .gz .tgz 下增長一行AddType application/x-httpd-php .php
/usr/local/apache2.4/bin/apachectl -t 測試配置文件的語法
/usr/local/apache2.4/bin/apachectl graceful 從新加載配置linux
Apache默認虛擬主機
vim /usr/local/apache2/conf/httpd.conf //搜索httpd-vhost,去掉#
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf //改成以下
<VirtualHost :80>
ServerAdmin admin@aminglinux.com
DocumentRoot "/data/wwwroot/aming.com"
ServerName aming.com
ServerAlias www.aming.com
ErrorLog "logs/aming.com-error_log"
CustomLog "logs/aming.com-access_log" common
</VirtualHost>
<VirtualHost :80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
</VirtualHost>git
域名跳轉
需求,把123.com域名跳轉到www.123.com,配置以下:
<VirtualHost :80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
<IfModule mod_rewrite.c> //須要mod_rewrite模塊支持
RewriteEngine on //打開rewrite功能
RewriteCond %{HTTP_HOST} !^www.123.com$ //定義rewrite的條件,主機名(域名)不是www.123.com,知足條件
RewriteRule ^/(.\)$ http://www.123.com/$1 [R=301,L] //定義rewrite規則,當知足上面的條件時,這條規則纔會執行 301指永久跳轉
</IfModule>
</VirtualHost>
/usr/local/apache2/bin/apachectl -M|grep -i rewrite //若無該模塊,須要編輯配置文件httpd.conf,刪除rewrite_module (shared) 前面的#github
訪問日誌
/usr/local/apache2.4/logs/ 日誌文件所在目錄
vim /usr/local/apache2.4/conf/httpd.conf //搜索LogFormat 能夠編輯日誌格式
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined user-agent 用戶代理(如瀏覽器) referer 訪問的上一網址(如經過超連接點進來,則顯示超連接所在網址;直接輸網址進入不顯示)
LogFormat "%h %l %u %t \"%r\" %>s %b" common
把虛擬主機配置文件改爲以下:
<VirtualHost :80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
CustomLog "logs/123.com-access_log" combined
</VirtualHost>
網站大多元素爲靜態文件,如圖片、css、js等,這些元素能夠不用記錄
把虛擬主機配置文件改爲以下:
<VirtualHost :80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
SetEnvIf Request_URI ".*\.gif$" img
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".*\.css$" img
CustomLog "logs/123.com-access_log" combined env=!img
</VirtualHost>
日誌切割
把虛擬主機配置文件改爲以下:
<VirtualHost :80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
SetEnvIf Request_URI ".*\.gif$" img
SetEnvIf Request_URI ".*\.jpg$" img
SetEnvIf Request_URI ".*\.png$" img
SetEnvIf Request_URI ".*\.bmp$" img
SetEnvIf Request_URI ".*\.swf$" img
SetEnvIf Request_URI ".*\.js$" img
SetEnvIf Request_URI ".\\.css$" img
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img // -l 以系統時間爲準 86400 一天有86400秒,意即以一天切割
</VirtualHost>
瀏覽器訪問網站的圖片時會把靜態的文件緩存在本地電腦裏,這樣下次再訪問時就不用去遠程下載了
增長配置
<IfModule mod_expires.c>
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/javascript "now plus 2 hours"
ExpiresByType application/x-shockwave-flash "now plus 2 hours"
ExpiresDefault "now plus 0 min" 其他文件不須要緩存
</IfModule>
須要在配置文件中打開expires_moduleredis
配置防盜鏈
從第三方引用網址,資源不是本身的,可是借用超連接取得資源點
經過限制referer來實現防盜鏈的功能
配置文件增長以下內容
<Directory /data/wwwroot/www.123.com> 被保護站點
SetEnvIfNoCase Referer "http://www.123.com" local_ref 白名單
SetEnvIfNoCase Referer "http://123.com" local_ref 白名單
SetEnvIfNoCase Referer "^$" local_ref 白名單,直接輸入網址的形式
<filesmatch ".(txt|doc|mp3|zip|rar|jpg|gif)">
Order Allow,Deny
Allow from env=local_ref
</filesmatch>
</Directory>
curl -e "http://www.aminglinux.com/123.html" 指定referer訪問
訪問控制
<Directory /data/wwwroot/www.123.com/admin/>
Order deny,allow //先執行下面的deny語句,再執行allow,allow的效果刷新,即除127.0.0.1外不容許訪問。若順序改變,則都不容許訪問
Deny from all
Allow from 127.0.0.1
</Directory>
<Directory /data/wwwroot/www.123.com>
<FilesMatch "admin.php(.*)">
Order deny,allow
Deny from all
Allow from 127.0.0.1
</FilesMatch>
</Directory>
<Directory /data/wwwroot/www.123.com/upload>
php_admin_flag engine off // 禁止解析php
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR] //OR表示跟下一個條件選擇關係(或者) NC表不區分大小寫
RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC]
RewriteRule .* - [F]
</IfModule>
PHP配置
/usr/local/php/bin/php -i|grep -i "loaded configuration file"
運行結果,能夠查到php的配置文件,命令結果:Loaded Configuration File => /usr/local/php/etc/php.ini
vim /usr/local/php/etc/php.ini
找到disable_functions = 在後面加上eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,leak,popepassthru,stream_socket_server,popen,proc_open,proc_close,表示這些函數被禁用
display_errors 表示錯誤會顯示出來
log_errors 錯誤日誌是否開啓
error_log 錯誤日誌文件及位置
error_reporting 記錄錯誤日誌等級
open_basedir 防止被黑,隔離目錄用,後跟被隔離目錄
也能夠在虛擬主機的配置文件里加上php_admin_value open_basedir "[目錄名]"