lamp(Web应用软件)
—LAMP指的Linux(操作系统)、ApacheHTTP 服务器,MySQL(有时也指MariaDB,数据库软件) 和PHP(有时也是指Perl或Python) 的第一个字母,一般用来建立web应用平台
l –>linux unix windows
a–> apache nginx iis
m–> mysql mariadb percona postgressql oracle
p–> php jsp xml python -> django falsk tornador css js api
—LAMP(Linux- Apache-MySQL-PHP)网站架构是目前国际流行的Web框架
nginx
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
–安装:
(1)server1:
tar zxf nginx-1.12.0.tar.gz 解压缩包
cd nginx-1.12.0/
useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin -u 800 nginx 创建用户,-M指定家目录
id nginx 查看用户,nginx的uid要保持一致
安装依赖性模块
yum install pcre-devel
yum install -y openssl-devel
cd nginx-1.12.0/src/core/
vim nginx.h 修改nginx版本名称
#define NGINX_VER "nginx"
nginx-1.12.0/auto/cc/gcc
cd nginx-1.12.0/
./configure –prefix=/usr/local/lnmp/nginx –user=nginx –group=nginx –with-threads –with-file-aio –with-http_ssl_module –with-http_stub_status_module 源码安装–配置
make && make install 源码安装–编译和安装
cd /usr/local/lnmp/nginx/sbin
./nginx 启动nginx
ln -s /usr/local/lnmp/nginx/sbin/nginx /sbin/ ##创建链接
测试:
curl -I localhost
nginx多核绑定
vim /usr/local/lnmp/nginx/conf/nginx.conf nginx主配置文件
worker_processes 2; ##进程数
worker_cpu_affinity 01 10; ##cpu绑定,01 10 表示第一二块cpu,须符合物理cpu数目
events {
worker_connections 65535; ##最大连接数
}
vim /etc/security/limits.conf 定义用户规则
nginx - nofile 65535 ##nofile-打开文件的最大数目
lscpu ##查看cpu
sysctl -a | grep file ##查看系统最大连接数
nginx -t ##检测nginx配置文件的语法是否正确
nginx -s reload ##nginx重新加载
测试:
[root@server1 conf]# usermod -s /bin/bash nginx
[root@server1 conf]# su - nginx
-bash-4.1$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 14868
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65535 ##打开的文件数变为65535
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 1024
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
nginx虚拟主机
vim /usr/local/lnmp/nginx/conf/nginx.conf 主配置文件
http {
server { ##指定主机和端口
listen 80;
server_name www.westos.org;
location / { ##匹配网页的路径
root /web1;
index index.html;
}
}
}
pwd :/usr/local/lnmp/nginx/conf
mkdir /web1 创建发布目录
vim /web1/index.html 编写页面内容
nginx -s reload ##重新加载
测试:
浏览器访问
www.westos.org –> 虚拟主机发布页面
172.25.59.11 –> nginx默认发布页面
nginx证书
vim /usr/local/lnmp/nginx/conf/nginx.conf
# HTTPS server
#
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem; ##指定PEM格式的证书
ssl_certificate_key cert.pem; ##指定PEM格式的密钥
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
redhat6版本生成证书
cd /etc/pki/tls/certs 可以在该目录下的Makefile查看生成证书的指令
make cert.pem 制作pem证书,写入证书内容
[root@server1 certs]# make cert.pem
-----
Country Name (2 letter code) [XX]:cn ##国家
State or Province Name (full name) []:shaanxi ##省份
Locality Name (eg, city) [Default City]:xi'an ##城市
Organization Name (eg, company) [Default Company Ltd]:westos ##组织名称
Organizational Unit Name (eg, section) []:linux ##单元名称
Common Name (eg, your name or your server's hostname) ##主名[]:server1
Email Address []:pucca@163.com ##邮箱
[root@server1 certs]# ll cert.pem
-rw------- 1 root root 3084 Jul 24 21:29 cert.pem
mv cert.pem /usr/local/lnmp/nginx/conf/ 将证书放在nginx的配置目录
nginx -t
nginx -s reload 重新加载服务
测试:
浏览器访问
https://172.25.59.11
监控nginx
vim /usr/local/lnmp/nginx/conf/nginx.conf 在默认主机里面加上location
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /status {
stub_status on; ##打开监控
access_log off; ##日志关闭
allow 127.0.0.1; ##允许本机
deny all; ##拒绝其他
}
#error_page 404 /404.html;
nginx -t
nginx -s reload
测试:
本机:curl localhost/status
其他主机访问:172.25.59.11/status
nginx网页重定向
vim /usr/local/lnmp/nginx/conf/nginx.conf
location / {
root /web1;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.westos.org;
rewrite ^(.*)$ https://www.westos.org$1 redirect; ##重写,redirect302临时,permanent301永久
}
}
nginx -t
nginx -s reload
测试:
浏览器访问 www.westos.org ,有安全认证
mkdir /web1/admin
vim /web1/admin/index.html 编写用户测试页
<h1>admin's page</h1>
浏览器访问:www.westos.org/admin
负载均衡,HTTP反向代理
apache服务关闭
server1:
vim /usr/local/lnmp/nginx/conf/nginx.conf
http {
upstream westos {##upstream配置块,加权轮询
#ip_hash
server 172.25.32.2:80; ##权重weigth 2,默认1
server 172.25.32.3:8080;
server 127.0.0.1:8000 backup;
}
server {
listen 80;
server_name www.westos.org;
#rewrite ^(.*)$ https://www.westos.org$1 permanent;
location / {
proxy_pass http://westos; ##HTTP反向代理模块
}
}
}
nginx -t
nginx -s reload
server3:
vim /etc/httpd/conf/httpd.conf 修改http端口
Listen 8000
/etc/init.d/httpd start 重启生效
测试:
[root@server1 html]# cat index.html 服务维护页面
当前网站正在维护中。。。。。
[root@server1 html]# nginx -s stop
[root@server1 html]# nginx
[root@server1 html]# for i in {1..10};do curl www.westos.org ; done
(server2/3 /etc/init.d/httpd stop ##后端服务挂掉时)
当前网站正在维护中。。。。。
当前网站正在维护中。。。。。
当前网站正在维护中。。。。。
当前网站正在维护中。。。。。
当前网站正在维护中。。。。。
当前网站正在维护中。。。。。
当前网站正在维护中。。。。。
当前网站正在维护中。。。。。
当前网站正在维护中。。。。。
当前网站正在维护中。。。。。
恢复之后:
[root@server1 html]# nginx
server2/3 /etc/init.d/httpd start