Nginx 未整理
Posted summer2
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx 未整理相关的知识,希望对你有一定的参考价值。
web服务器:
httpd,nginx,tengine
tomcat,jboss,websphere
tar -xf nginx-1.10.3.tar.gz
cd nginx-1.10.3
yum -y install gcc pcre-devel openssl-devel
useradd -s /sbin/nologin nginx
./configure --user=nginx --group=nginx --with-http_ssl_module
make --->objs源码包目录下 如同QQ安装包,如果删掉也没有关系
make install (复制拷贝)--->/usr/local/nginx
/usr/local/nginx/sbin/nginx 启动
/usr/local/nginx/sbin/nginx -s stop 关闭
/usr/local/nginx/sbin/nginx -s reload 不关服务重新更新配置
/bin/ /sbin/ /usr/bin/ /usr/sbin/
ln -s /usr/local/nginx/sbin/nginx /sbin/ 创建软链接
tar -xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --user=nginx --group=nginx --with-http_ssl_module
make
在源码包目录下有个叫objs/nginx (绿色可执行程序nginx-1.12.2)
mv /usr/local/nginx/sbin/ngigx{,.bak}
cp objs/nginx /usr/local/nginx/sbin/
启动
杀死老程序进程
killall nginx
nginx
nginx -V
vim /usr/local/nginx/conf/nginx.conf
全局配置(用户,进程数量,日志,并发量)
http{
server{
listen 80;
server_name localhost;
root html;
index index.html;
}
}
监控 boss【web】
用户认证
vim /usr/local/nginx/conf/nginx.conf
http{
server{
listen 80;
server_name localhost;
auth_basic "xx:";
auth_basic_user_file "/usr/local/nginx/pass";
root html;
index index.html;
}
}
yum -y install httpd-tools
htpasswd -c /usr/local/nginx/pass tom
>123456
>123456
htpasswd /usr/local/nginx/pass jerry
>123456
>123456
nginx -s reload
基于域名的访问
vim /usr/local/nginx/conf/nginx.conf
http{
server{
listen 80;
server_name www.a.com;
auth_basic "xx:";
auth_basic_user_file "/usr/local/nginx/pass";
root html;
index index.html;
}
server{
listen 80;
server_name www.b.com;
root www;
index index.html;
}
}
mkdir /usr/local/nginx/www
echo "xx" > /user/local/nginx/www/index.html
nginx -s reload
客户端:
vim /etc/hosts
192.168.4.5 www.a.com www.b.com
firefox www.a.com
firefox www.b.com
1.安装
2.升级
3.认证
4.虚拟主机
5.https (http+ssl)
私钥,公钥=证书
-------------------------------------------------------------------------
安装1.10
tar -xf nginx-1.10-3.tar.gz 解包
cd /root/lnmp_soft/nginx-1.10.3 cd到目录下
yum -y install gcc pcre-devel openssl-devel 装3个依赖包
useradd -s /sbin/nologin nginx 创建依赖的同名用户
./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx --with-http_ssl_module 配置选项
make 编译
make install 安装
ln -s /usr/local/nginx/sbin/nginx /sbin/ 创建软链接
nginx 启动
-----------------------------------------------------------------------
升级1.12
tar -xf nginx-1.12.2.tar.gz 解包
cd /root/lnmp_soft/nginx-1.12.2 cd到目录下
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module 配置选项
make 编译!就行!不要安装!
mv /usr/local/nginx/sbin/nginx{,.bak} 备份
cp objs/nginx /usr/local/nginx/sbin/ 拷贝到解释器下
killall nginx 杀掉老nginx进程
nginx 启动
nginx -V 查看新版本
vim /usr/local/nginx/conf/nginx.conf
http{
server{
listen 80; 监听端口
server_name web1.example.com; 域名
location / { location判断
root /web1; 根目录
index index.html; 起始页
}
}
}
----------------------------------------------------------------------
用户认证
改配置文件(在里面添加)
vim /usr/local/nginx/conf/nginx.conf
auth_basic "xx:"; 认证框提示信息
auth_basic_user_file "/usr/local/nginx/pass"; 认证文件路径
yum -y install httpd-tools 安装认证依赖包
htpasswd -c /usr/local/nginx/pass tom 创建认证用户,-c是创建
htpsswd /usr/local/nginx/pass jerry 创建认证用户,不要加c创建新文件,否则会覆盖
nginx -s reload 重新加载
----------------------------------------------------------------------
基于域名的访问
vim /usr/local/nginx/conf/nginx.conf
...
server{
listen 80;
server_name web1.example.com;
locatin / {
root html;
index index.html;
auth_basic "Input Password:";
auth_basic_user_file "xxx";
}
}
server{
listen 80;
server_name web2.example.com;
location /{
root /web2;
index index.html;
}
}
mkdir /usr/local/nginx/web2
echo "web2" > /usr/local/nginx/web2/index.html
nginx -s reload
客户端:
echo "192.168.4.5 web1.example.com web2.example.com" > /etc/hosts
firefox web1.example.com
firefox web2.example.com
-----------------------------------------------------------------
部署公钥私钥
cd /usr/local/nginx/conf
openssl genrsa > cert.key
openssl req -new -x509 -key cert.key > cert.pem
请求 新的证书 类型 私钥 私钥名字 公钥名字
写国家,省份,城市,公司,部门,姓名,邮箱
ls
------------------------------------------------------------------
安全web!!!https!!!
修改98,115行。只修改域名web3.example.com
nginx -s reload
客户端:
vim /etc/hosts
添加 192.168.4.5 web3.example.com
firefox https://web3.example.com https!!!
--------------------------------------------------------------------
问题:nginx如果未加模块ssl,会报错!
cd /root/lnmp_soft/nginx-1.12(tab)
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
make
mv /usr/local/nginx/sbin/nginx{,.bak}
cp objs/nginx /usr/local/nginx/sbin/nginx
nginx -s reload
nginx -V
---------------------------------------------------------------------
LNMP
linxu
nginx
mysql,mariadb
php,python
安装3个系列包:
nginx
mariadb,mariadb-server,mariadb-devel
php,php-fpm,php-mysql #要记住是php-mysql
cd /root/lnmp_soft/
yum -y install mariadb mariadb-server mariadb-devel
yum -y install php php-fpm php-mysql
启动3个服务:
nginx #如果已开启,就不用再开了。如果开启了httpd,要先关闭httpd,因为80端口只能由一个软件来用。
systemctl start mariadb
systemctl start php-fpm #要记住是启动php-fpm
防火墙设为trusted
firewall-cmd --set-default-zone=trusted
setenforce 0
----------------------------------------------------------------------
验证是否3个服务是否已启动
netstat antulp | grep 80
netstat antulp | grep 3306
netstat antulp | grep 9000
----------------------------------------------------------------------
写一个php脚本
cd /usr/local/nginx/html
ls
echo "
<?php
$i=33; #要记住有;结尾
echo $i;
?>
" > test.php
验证php是否可以解释这个脚本
#php test.php
------------------------------------------------------------------
nginx /etc/rc.local(是开机运行的脚本,是后面的链接)---> /etc/rc.d/rc.local
vim /etc/rc.local
# Please note that you must run ‘chmod +x /etc/rc.d/rc.local‘ to ensure
# that this script will be executed during boot
/usr/local/nginx/sbin/nginx
chmod +x /etc/rc.d/rc.local
systemctl start mariadb
systemctl enable mariadb
systemctl start php-fpm
systemctl enable php-fpm
nginx动静分离,把php的处理转发给9000(php=tpm)
location /{
root html;
}
loction ~ \\.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
include fastcg.conf;
}
nginx -s reload
编写脚本php,链接数据库,对数据库操作
地址重写
a.html --------> b.html
rewrite /a.html /b.html;
也可以rewrite /a.html /test/b.html;
----------------------------------------------------
192.168.4.5----->www.tmooc.cn
rewrite ^/ http://www.tmooc.cn;
-----------------------------------------------------
192.168.4.5/*----->www.tmooc.cn/*
rewrite ^/(.*) http://www.tmooc.cn/$1;
#########################################################################################
动静分离(nginx)
location匹配用户的地址栏
location支持正则
如果找不到,它会匹配/,因为所有的东西都是从/下面开始的。
#auth_basic "Input Password:";
#auth_basic_user_file "/usr/local/nginx/pass";
下面这句话是错的,注释掉或者删掉。正确的文件在fastcgi.conf,就如同httpd的调用配置文件一样
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
location ~ \\.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf; #修改此处的文件名
}
# nginx -s reload
客户端client测试访问
# firefox www.a.com/test.php
---------------------------------------------------------------------
配置文件里面的基本格式:
<Server>
<Service>
<Connector port=8080 />
<Connector port=8009 />
<Engine name="Catalina" defaultHost="localhost">
<Host name="www.a.com" appBse="a" unpackWARS="true" autoDeploy="true">
</Host>
<Host name="www.b.com" appBase="b" unpackWARS="true" autoDeploy="true">
</Host>
</Service>
</Server>
-------------------------------------------------------------------------------------------
可参考
http://www.cnblogs.com/junneyang/p/5231849.html
CDN: content delivery network
tmooc【集群】
上海【缓存】varnish
南京【缓存】
广州【缓存】
--------------------------------------------------------------------------------------------------
192.168.4.5(LNMP)
# systemctl restart mariadb
# systemctl enable mariadb
# ss -ntulp | grep 80
# ss -ntulp | grep 3306
# ss -ntulp | grep 9000
# ss -ntulp | grep 11211
# vim /usr/local/nginx/conf/nginx.conf
# vim /usr/local/nginx/html/test.php
<?php
$i=33;
echo $i;
?>
# nginx -s reload
# firefox http://192.168.4.5/test.php
可以看到显示33
----------------------------------------------------------------------
构建memcached服务
安装Memcached服务
启动服务并查看网络连接状态验证是否开启成功:
关闭SELinux、防火墙(虚拟机默认是关闭的)
# yum list | grep memcache
# yum -y install php-pecl-memcache
# systemctl restart php-fpm
# firefox 192.168.4.5/mem.php
可以看到显示test
------------------------------------------------------------------------
如果点击登陆没有反应,是没有安装php。安装后重启。
以上是关于Nginx 未整理的主要内容,如果未能解决你的问题,请参考以下文章