一、Nginx基础
<安装Nginx软件包>
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/sysconfig/selinux
[root@localhost ~]# tar zxvf nginx-1.12.0.tar.gz
[root@localhost nginx-1.12.0]# yum -y install pcre-devel zlib-devel gcc* openssl-devel
openssl-devel 利用https做网站进行加密
[root@localhost nginx-1.12.0]# useradd -M -s /sbin/nologin nginx (创建程序用户。-M不创建home目录,-s 登录shell)
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make && make install
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/(为主程序创建链接文件,方便直接调用nginx)
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx (启动)
基础命令:
[root@localhost ~]# killall -s HUP nginx
= [root@localhost ~]# killall -1 nginx
//重载,选项 -s HUP 等同于 -1
关闭:
[root@localhost ~]# killall -s QUIT nginx
[root@localhost ~]# killall -3 nginx
//关闭,选项 -s QUIT 等同于 -3
nginx 开启
[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: 35 90 20
#description:my nginx
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
pkill nginx
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "!!!"
exit 1
esac
exit 0
[root@localhost init.d]# chmod +x nginx (为nginx添加执行权)
[root@localhost init.d]# chkconfig --add nginx (添加为系统服务,必须在init.d目录下)
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server_name www.benet.com; 网站名称
charset utf-8; 字符集
location /status { 访问位置为/status
stub_status on; 打开状态统计功能
access_log off; 关闭日志记录
}
二、Nginx访问控制
[root@localhost ~]# yum -y install httpd-tools
[root@localhost ~]# htpasswd -c /usr/local/nginx/pass.db zhangsan (添加账户-c为创建)
[root@localhost nginx]# chmod 400 pass.db (为pass.db提供写的权限)
[root@localhost nginx]# chown nginx pass.db (更改属主)
-r--------. 1 nginx root 47 6月 17 18:50 pass.db
[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/pass.db;
deny 192.168.10.102;拒绝192.168.10.2ip访问,其余都允许。
allow all;
[root@localhost nginx]# nginx -t
[root@localhost nginx]# systemctl restart nginx
三、Nginx虚拟主机
1.基于域名
server {
listen 80;
#listen somename:8080;
server_name www.accp.com;
access_log logs/accp.com.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.benet.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/html/benet;
index index.html index.htm;
[root@localhost nginx]# mkdir -p /var/www/html/benet
[root@localhost nginx]# mkdir -p /var/www/html/accp
[root@localhost accp]# echo "www.accp.com">index.html
[root@localhost accp]# cd ../benet
[root@localhost benet]# echo "www.benet.com">index.html
102
[root@localhost ~]# vim /etc/hosts
192.168.10.101 www.accp.com
192.168.10.101 www.benet.com
102测试
[root@localhost ~]# curl www.accp.com
www.accp.com
[root@localhost ~]# curl www.benet.com
www.benet.com
[root@localhost ~]#
2.基于IP
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:1
[root@localhost network-scripts]# vim ifcfg-ens33:0
IPADDR=192.168.10.210
NAME=ens33:0
DEVICE=ens33:0
[root@localhost network-scripts]# vim ifcfg-ens33:1
IPADDR=192.168.10.211
NAME=ens33:1
DEVICE=ens33:1
server {
listen 192.168.10.211:80;
#listen somename:8080;
server_name www.accp.com;
access_log logs/accp.com.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
}
server {
listen 192.168.10.210:80;
server_name www.benet.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/html/benet;
index index.html index.htm;
102测试
[root@localhost ~]# curl 192.168.10.211
www.accp.com
[root@localhost ~]# curl 192.168.10.210
www.benet.com
3.基于端口
server {
listen 192.168.10.101:80;
#listen somename:8080;
server_name www.accp.com;
access_log logs/accp.com.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
}
server {
listen 192.168.10.210:8080;
server_name www.benet.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/html/benet;
index index.html index.htm;
102测试
[root@localhost ~]# curl 192.168.10.101:80
www.accp.com
[root@localhost ~]# curl 192.168.10.101:8080
www.benet.com
四、LANMP
[root@localhost ~]# yum -y install mariadb-server mariadb (利用yum安装)
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# mysqladmin -uroot password 'pwd123' (设置密码)
[root@localhost ~]# mysql -uroot -ppwd123 (方式一,明码登录)
[root@localhost ~]# mysql -uroot -p (方式二,暗码登录)
Enter password:
<安装php>
[root@localhost ~]# tar zxvf php-5.5.38.tar.gz
[root@localhost ~]# yum -y install gd libxml2-devel libjpeg-devel libpng-devel gcc*
备注:
gd:支持图形文件
libxml2-devel 支持xml文件格式
libjpg:支持jpg图像格式
libpng:支持png图像格式
[root@localhost php-5.5.38]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm
[root@localhost php-5.5.38]# make && make install
1. Nginx支持PHP环境
(1)启动 php-fpm 进程
[root@localhost php-5.5.38]# cp php.ini-development /usr/local/php5/php.ini (php配置文件)
备注:
/usr/local/php5/ 与configure中./configure --prefix=/usr/local/php5 相一致
php.ini 重命名
[root@localhost php-5.5.38]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[root@localhost php-5.5.38]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/
[root@localhost php-5.5.38]# cd /usr/local/php5/etc/ (php-fpm)
[root@localhost etc]# ls
pear.conf php-fpm.conf.default
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# useradd -M -s /sbin/nologin php
[root@localhost etc]# vim php-fpm.conf
user = php (修改 150)
group = php
pm.max_children = 50 (最大进程数)
pm.start_servers = 20 (启动该进程时最大服务数量)
pm.min_spare_servers = 5 (最少空闲进程数)
pm.max_spare_servers = 35 (最大空闲数)
[root@localhost etc]# /usr/local/sbin/php-fpm (启动该进程)
php是独立运行的状态
[root@localhost etc]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: 35 90 20
#description:my nginx
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx/pid"
PROG_FPM="/usr/local/sbin/php-fpm"
PIDF_FPM="/usr/local/php5/var/run/php-fpm.pid"
case "$1" in
start)
$PROG
$PROG_FPM
;;
stop)
pkill nginx
pkill -9 php-fpm
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
kill -s HUP $(cat $PIDF_FPM)
;;
*)
echo "!!!"
exit 1
esac
exit 0
[root@localhost etc]#systemctl daemon-reload (重载)
(2)PHP页面访问
静态页面:
[root@localhost ~]# mkdir -p /var/www/html
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.html
my test
动态页面:
[root@localhost html]# vim test.php
<?php
phpinfo();
?>
方法一:利用102进行反向解析
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root /var/www/html;
location ~ \.php$ { (正则表达式 66)
proxy_pass http://192.168.10.102; ( 利用102进行反向解析)
}
[root@localhost html]# systemctl restart nginx
102
[root@localhost html]# yum -y install httpd mariadb-server mariadb php php-mysql
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim text1.php
方法二:
server {
listen 80;
server_name www.benet.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /var/www/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /var/www/ html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}