在centos 7上搭建web常用軟件和優化
本文主要是針對web服務的,生產環境會使用的一些軟件,只是簡單陳述,如果有什麽不正確的地方,請指教,謝謝!
Web搭建的軟件:httpd,nginx,tomcat
代理軟件:squid,varnish
數據庫軟件:mariadb,redis,mencache
驗證瀏覽方式:elinks -dump, curl,firefox 等
查看web網絡監聽: netstat/ss -anptu | grep httpd/80 註:建議使用ss,速度快
URL(Uniform Resource Locator)組成:http://服務器地址『:端口號』/目錄/文件名
LNMP 註:LAMP A:apache
L:linux
N:nginx
M:mariadb、mariadb-server、mariadb-devel
P:php、php-fpm、php-mysql
===========================================================================
- HTTP
軟件包:httpd (apache www.apache.org)
安裝方式:yum
服務:httpd
傳輸協議及端口:TCP 80 註:https TCP 443 軟件包:mod_ssl
配置文件:
主配置文件:/etc/httpd/conf/httpd.conf 註:一般不修改
其他:/etc/httpd/conf.d/*.conf
默認首頁:index.html
默認根目錄:/var/www/html
啟動服務:systemctl restart httpd systemctl enable httpd
配置文件內容:
Listen 端口號 註:添加新的Web端口 semanage port -a -t http_port_t -p tcp 端口號
ServerName 網站的FQDN 註:可以基於域名的虛擬主機,使用最多
DocumentRoot 網站的網頁根目錄
WSGIScriptAlias / /網站的網頁網頁 註:需要mod_wsgi模塊軟件包
</VirtualHost>
<Directory "目錄路徑">
Require all denied ??????????????????????????????? ?//上層目錄拒絕任何訪問
Require all granted //目錄允許任何訪問
Require ip IP或網段地址 .. .. //目錄允許少數客戶機
</Directory>
==================================================================================
- NGINX (engine x)
軟件包:nginx (http://nginx.org) 註:依賴包pcre-devel openssl-devel
安裝方式:源碼安裝 註:依賴包gcc
傳輸協議及端口:TCP 80 註:開啟nginx時,需要關閉httpd-- killall httpd
配置文件:/usr/local/nginx/conf/nginx.conf
默認首頁:index.html
默認根目錄:/usr/local/nginx/html/
啟動服務:
/usr/local/nginx/sbin/nginx //啟動服務
/usr/local/nginx/sbin/nginx -s stop //關閉服務
/usr/local/nginx/sbin/nginx -s reload //重新加載配置文件 相當於重啟
配置文件內容:
server{
listen 80; //端口
server name localhost; //域名
location /{
root html; //指定網站根目錄
index index.html index.htm; //默認首頁
rewrite /a.html /b.html; //訪問a.html重定向到b.html
}
}
Nginx反向代理(很重要):
http {
upstream webserver { //配置upstream服務器集群池屬性
server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10; //權重1,失敗次數1,超時10
server 192.168.2.200 weight=1 max_fails=2 fail_timeout=10;
}
server {
listen 80;
server_name www.aa.com;
location / {
proxy_pass http://webserver; //實現反向代理功能
}
}
}
Nginx優化:
worker-processes 1; //與cpu核心數一致 查看cpu用lscpu
events {
worker_connection 65535; //每個worker最大並發連接數
}
http {
server_tokens off; //不顯示nginx版本號信息
client_header_buffer_size 1k; //默認請求包頭信息的緩存
large_client_header_buffers 4 4k; //大請求包頭部信息的緩存個數與容量
gzip on;
gzip_min_length 1000;
gzip_comp_level 4;
gzip_types text/plain
}
Server {.................省略}
Location ~* .(jpg|jpeg|gif|png|css|js|ico|xml)$(
Expires 30d; //定義客戶端緩存時間為30天
)
修改Linux操作系統最大打開文件數
#vim /etc/security/limits.conf (永久配置)
- soft nofile 100000
- hard nofile 100000
linux系統上需要設置的(臨時設置)
ulimit –Hn 100000
ulimit –Sn 100000
====================================================================
3.TOMCAT
軟件包:apache-tomcat-8.0.30.tar.gz 註:依賴包JDK java-1.8.0-openjdk
安裝方式:解壓後拷貝到/usr/local/tomcat/ 則可以使用
傳輸協議及端口:TCP 8080 https 8443
配置文件:/usr/local/tomcat/conf/server.xml
頁面目錄:/usr/local/tomcat/webapps/ROOT/
默認首頁:index.html
啟動服務:
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
配置文件內容:
<Host name=www.aa.com appBase="aa" unpackWARS="true" autoDeploy="true">
<Context path="" docBase="base" />
</Host>
====================================================================
- SQUID
軟件包:squid
安裝方式:yum
傳輸協議和端口:TCP 80
配置文件:/etc/squid/squid.conf
啟動服務:
systemctl start squid;systemctl enable squid
配置文件內容:
http_port 80 vhost //設置反向代理
visible_hostname svr5.tarena.com //設置主機名,默認沒有該語句
cache_peer 192.168.2.100 parent 80 0 originserver //定義後端真實服務器信息
cache_dir ufs /var/spool/squid 200 16 256 //硬盤緩存,緩存容量為200M,自動創建16個一級子目錄和256個二級子目錄
http_access allow all //允許本機所有主機使用代理服務器
==========================================================
- VARNISH
軟件包:varnish-3.0.6.tar.gz 註:依賴包read-devel pcre-devel
安裝方式:源碼安裝
傳輸協議和端口:TCP 80
配置文件:
/etc/sysconfig/varnish
/etc/varnish/default.vcl
啟動服務: service varnish start
配置文件內容:
vim /etc/sysconfig/varnish
66行:VARNISH_LISTEN_PORT=80 //默認端口
89行:VARNISH_STORAGE_SIZE=64M //定義緩存大小
92行:VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}" //基於內存方式緩存
linux系統處理過程:
mkdir /etc/varnish
cp /usr/local/varnish/etc/default.vcl /etc/varnish/
uuidgen > /etc/varnish/secret
vim /etc/varnish/default.vcl
配置內容
backend default {
.host = "192.168.2.100";
.port = "80";
}
==========================================================
在centos 7上搭建web常用軟件和優化