LNMP搭建
1.安装PHP
① 之前安装过lamp,所以要在 /usr/local/src/php 的目录下进行 make clean
② 在上面的目录下编译:
./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-jepg-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --with-mbstring --enable-exif --disable-ipv6 --with-pear
③ cp php.ini-production /usr/local/php-fpm/etc/php.ini 并且配置 /usr/local/php-fpm/etc/php.ini 文件,内容如下:
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
此时会报错说 php-fpm.conf 不存在,只需要将 php-fpm.conf.default 复制为 php-fpm.conf 即可。
通过 /usr/local/php-fpm/sbin/php-fpm -t 检测,出现 test successful 说明成功。
④ 启动 php-fpm
cp /usr/local/src/php.5.6/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
useradd -s /sbin/nologin php-fpm
service php-fpm start
chkconfig php-fpm on
ps aux | grep -i php-fpm
2.安装 Nginx
① 下载并解压源码包
② 进入解压包目录并配置编译选项:
cd nginx-1.10.3
./configure --prefix=/usr/local/nginx
③ 编译并安装
make && make install
④ 编写 nginx 启动脚本
vim /etc/init.d/nginx //内容如下:
#!/bin/sh
#
#nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
⑤ 更改启动脚本的权限,并设置开机自启 nginx
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
⑥ 更改配置文件
> /usr/local/nginx/conf/nginx.conf //将 nginx 中的内容清空,配置如下内容:
user nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections 6000;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_max_size 4096;
server_names_hash_bucket_size 3526;
log_format combined_relip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent" ';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/clent_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_http_version 1.1;
gzip_comp_level 5;
gzip_types text/plain application/x-javascript text/css application/xml text/htm;
server
{
listen 8080;
server_name localhost;
client_max_body_size 100M;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
include vhost/*.conf;
}
*** include vhost/*.conf; 意思是当前目录的 vhost 下的所有的 .conf 文件会加载。
2. 默认虚拟主机
nginx 中的默认虚拟主机有单独的字段标记,即有 default_server 标识的主机就是默认虚拟主机,而在 httpd 中默认第一个虚拟主机是默认虚拟主机。
① 建立虚拟主机的存放目录,和默认虚拟主机的文件
mkdir /usr/local/nginx/conf/vhost
touch /usr/local/nginx/conf/vhost/default.conf
② 配置 default.conf
server{
listen 8080 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /usr/local/ngnix/docs/default;
}
③ 建立网站网页存放地
mkdir /usr/local/nginx/docs/default
echo "hello world" > /usr/local/nginx/docs/default/test.html
④ 检查并输出网页内容
/usr/local/ngnix/sbin/nginx -t //检查配置是否正确,出现 “OK”、“successful” 说明正确
/usr/local/nginx/sbin/nginx -s reload //重新加载
curl -xlocalhost:8080 aaa.com //输出页面内容
3. 配置用户认证
① 编写主机的配置文件
server{
. . . //虚拟主机的基本配置内容
location /
{
auth_basic "Auth"; //打开用户认证
auth_basic_use_file "/usr/local/nginx/conf/htpasswd"; //指定用户密码文件
}
② 创建用户
htpasswd -cm /usr/local/nginx/conf/htpasswd gxnginx //创建用户名为 gxnginx 的用户
③ 检验
curl -xlocalhost:8080 -I aaa.com //若状态码为 401 ,成功!
4. 配置域名跳转
① 配置以下文件的以下内容:
vim /usr/local/nginx/conf/vhost/test1.conf
server{
listen 8080;
server_name test1.com testt1.com;
index index.html index.htm index.php;
root /usr/local/ngnix/docs/test1;
if ( $host != "test1.com" ){
rewrite /(.*)/ http://test1.com/$1 permanent; //permanent 是 301,redirect 是 302.
}
}
【注意事项】 以上代码中的 if 语句,if 和括号之间有空格,不然不识别。该域名跳转案例的含义是:如果你输入的域名和 test1.com 不相等,则跳转到 test1.com。
5. 配置访问日志
① 查看访问日志的格式
# grep -A2 log_format /usr/local/nginx/conf/nginx.conf // 内容如下:
log_format combined_realip【日志格式标识】
'$remote_addr【访问网站的出口IP】 $http_x_forwarded_for【代理服务器的idp】 [$time_local]'【访问时间】
' $host【访问的主机名】 "$request_uri"【访问的URL地址】 $status'【状态码】
' "$http_referer"【referer】 "$http_user_agent"【浏览器信息】 ';
# cat /usr/local/nginx/logs/access/default.log //内容如下:
192.168.187.1 - [30/Jan/2021:10:49:08 +0800] 192.168.187.129 "/aaa.com" 404 "-" "Mozilla/5.0 (Windows) Gecko/20100101 Firefox/84.0"
[注意] cat 和 grep 的内容是对应关系!
② 配置虚拟主机的配置文件
server{
. . . //基础配置
access_log /usr/local/ngnix/logs/access/defualt.log combined_realip;
}
[格式] access_log 日志存放地+日志名 日志格式标识 ;
③ 配置日志切割脚本
# vim /usr/local/nginx/sbin/nginx_log_rotate.log
#!/bin/bash
d=`date -d "-1 day" +%Y%m%d`
logdir="/usr/local/nginx/logs/access"
nginx_pid="/usr/local/nginx/logs/ngin.xpid"
cd $logdir
for log in `ls *.log`
do
mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
⑤ 日志过期时间的配置
6. 防盗链
7. 配置访问控制
vim /usr/local/nginx/conf/vhost/default
location /admin/{
allow 127.0.0.1;
allow 192.168.187.129;
deny all;
}
[解释] 在 /admin/ 的目录下只允许 127.0.0.1 和 192.168.197.129 访问。
8. Nginx 解析 PHP
在 LAMP 中 PHP 作为 httpd 的一个模块,所以不用去单独配置 httpd 解析 PHP,因为在安装 httpd 和 PHP 的时候,就让他们建立了联系。然而在 LNMP 中 PHP 和 nginx 相互独立,所以需要通过进行配置来让他们互相能够传递信息。配置内容如下:
location ~ /*.php{
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/docs/default$fastcgi_script_name;
}
[解释1] fastcgi_pass 后面的值和 /usr/local/php-fpm/etc/php-fpm.conf 里的 listen = 后面的值一样。
[解释2] /usr/local/nginx/docs/default 这个值和 root 后面的值相同。
[解释3] 除以上两处改动外,其他的都不变。
9. Nginx 代理
① 什么是代理?
代理就是指路人,中间商。
② 配置代理:
server{
listen 8080;
server_name www.test.com;
location /{
proxy_pass http://121.201.9.155/; //所代理的ip
proxy_set_Header host $host;
proxy_set_Header X-Real-IP $remote_addr;
proxy_set_Header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
③ 负载均衡:
upstream qq_com{
in_hash;
server IP;
server IP;
}
server{
...
location /{
proxy_pass qq_com;
...
}
}
10. php-fpm 的配置
① 全局配置
# vim /usr/local/php-fpm/etc/php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
include = etc/php-fpm.d/*.conf
② 其他 pool 文件配置
[www]
listen = /tmp/www.sock //和 nginx 的配置文件对应
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic //设置为多线程模式
pm.max_children = 50 //限定线程最大个数
pm.start_servers = 20 //设置服务开启时的线程数
pm.min_spare_servers = 5 //最少空闲进程数
pm.max_spare_servers = 35 //最大空闲进程数
pm.max_requests = 500 //一个线程最多处理的请求数
rlimit_files = 1024
//设置慢执行日志
request_slowlog_timeout = 1 //设置请求超过一秒建立慢执行日志
slowlog = /usr/local/php-fpm/var/log/www-slow.log //慢执行日志的路径
php_admin_value[open_basedir] = 目录路径:目录路径 //只允许这两个目录可以解析PHP。