LNMP环境搭建与配置(1)

LNMP环境搭建与配置

安装mysql
下载软件包

# cd /usr/local/src/
#wget
http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.
gz

解压

#tar zxf源码包
#tar zxvf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.6.39-linux-glibc2.12-x86_64/usr/local/mysql

安装和配置

# useradd -s /sbin/nologin mysql
//建立MySQL用户,因为启动MySQL需要该用户
# mkdir -p /data/mysql
//创建datadir,数据库文件会放到这里面
# chown -R mysql:mysql /data/mysql
//更改权限,不更改后续操作就会出问题
#yum install -y perl-Module-Install
# cd /usr/local/mysql
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
//--user表示定义数据库的以哪个用户的身份运
//--datadir表示定义数据库的安装目录
#cp support-files/my-default.cnf/etc/my.cnf
#vim /etc/my.cnf
修改
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 131
socket = /tmp/mysql.sock
#cp support-files/mysql.server/etc/init.d/mysqld
//复制启动脚本文件
#chmod 777 /etc/init.d/mysqld
//修改启动脚本文件的属性
#vi /etc/init.d/mysqld
basedir = /usr/local/mysql
datadir=/data/mysql
//修改启动脚本
#chkconfig --add mysqld
//把mysql服务加到系统服务列表中
#chkconfig mysqld on
service mysqld start
//开机就启动

安装php

下载资源包

# wget http://php-5.6.30.tar.gz

创建目录

# cd /usr/local/src

解压

# tar -zxvf php-5.6.30.tar.gz

3.安装配置

# yum install -y libxml2-devel openssl openssl-devel bzip2 bzip2-devel libpng libpng-devel freetype freetype-devel epel-release libmcrypt-devel libjpeg-turbo libjpeg-turbo-devel

# yum install -y libm crypt-devel
//为防止配置时出错提前安装库文件
#cd php-5.6.30 
//cd命令:切换当前目录至其它目录,比如进入/etc目录,则执行 cd /etc。
#yum install bzip2-devel.x86_64-y
#./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs  --with-config-file-path=/usr/local/php/etc         --with-mysql=/usr/local/mysql --with-libxml-dir --with-gd--with-jpeg-dir --with-png-dir--with-freetype-dir  --with-iconv-dir--with-zlib-dir--with-bz2--with-openssl--with-mcrypt--enable-soap  --enable-gd-native-ttf--enable-mbstring--enable-sockets--enable-exif

编译 安装

# make
# make install
//编译安装

修改配置文件

#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
[www]
user=php-fpm
group=php-fpm
listen=/tmp/php-fcgi.sock
listen.mode=666
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
#/usr/local/php-fpm/sbin/php-fpm-t
tsetissuccessful//代表成功
#cd/usr/local/src/php-5.6.30
#cpsapi/fpm/init.d.php-fpm/etc/init.d/php-fpm
#chmod 755/etc/init.d/php-fpm
#chkconfig --addphp-fpm
chkconfigphp-fpmon
cd/usr/local/php-fpm/
sbin/php-fpm -t
servicephp-fpmstart
ps -ef|grepphp
ll /tmp/php-fcgi.sock

测试安装
#psaux|grepphp-fpm

安装Nginx

下载

#cd /usr/local/src/

解压

#wget http://nginx.org/download/nginx-1.16.1.tar.gz
#tar zxf nginx-1.16.1.tar.gz

配置安装

#cd nginx-1.16.1/
#./configure --prefix=/usr/local/nginx
#make && make install
#vi /etc/init.d/nginx

修改配置文件

#!/bin/bash
#chkconfig: -30 21
#description:http service.
#Source Function Library
./etc/init.d/functions
#Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n$"Starting $prog:"
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN-c$NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n$"Stopping $prog:"
killproc -p $NGINX_PID$NGINX_SBIN -TERM
rm -rf/dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo-n$"Reloading$prog:"
killproc -p$NGINX_PID$NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN-c$NGINX_CONF -t
return0
}
case"$1"in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage:$0{start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
#chmod 755/etc//init.d/nginx
#chkconfig --addnginx
#chkconfig nginx on
#> /usr/local/nginx/conf/nginx.conf
#vim /usr/local/nginx/conf/nginx.conf

修改配置文件

user nobody nobody;
worker_processes 2;
error_log/usr/local/nginx/logs/nginx_error.logcrit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
usee poll;
worker_connections6000;
}
http
{
include mime.types;
default_typeapplication/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip'$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_buffers4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path/usr/local/nginx/client_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_buffers4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_typestext/plainapplication/x-javascripttext/csstext/htm
application/xml;
server
{
listen 80;
server_namelocalhost;
indexindex.htmlindex.htmindex.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_passunix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_paramSCRIPT_FILENAME/usr/local/nginx/html$fastcgi_script_name;
}
}
}
#/usr/local/nginx/sbin/nginx-t
#vim /usr/local/nginx/conf/nginx.conf
#/usr/local/nginx/sbin/nginx-t
#service nginx start
#ps aux|grep nginx

测试是否正确解析

#vi /usr/local/nginx/html/2.php
<?php
echo"testphpscripts";
?>

testphpscripts//证明解析成功

Nginx配置

默认虚拟主机
配置
首先修改配置文件

#vi /usr/local/nginx/conf/nginx.conf

在最后一个结束符号}前加一行配置:

include vhost/*.conf;
//意思就是/usr/local/nginx/conf/host下面的所有以.conf结尾的文件都会被加载
#mkdir /usr/local/nginx/conf/vhost
#cd /usr/local/nginx/conf/vhost
#vim default.conf

server

{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root/data/nginx/default;
}

/usr/local/nginx/sbin/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
#/usr/local/nginx/sbin/nginx -s reload
mkdir -p /data/nginx/default
#echo "default_server">/data/nginx/default/index.html

//创建索引页
检验测试

#curl -x127.0.0.1:80 aaa.com
//访问aaa.com
default_server
#curl -x127.0.0.1:80 1212.com
//访问一个没有定义过的域名,也会访问aaa.com

测试成功

default_server

用户认证

配置
再来创建一个新的虚拟主机

#cd /usr/local/nginx/conf/vhost
#vi test.com.conf
修改配置文件
server
{
listen 80;
server_nametest.com;
index index.html index.htm index.php;
root /data/nginx/test.com;
location/
{
auth_basic"Auth";
//打开认证
auth_basic_user_file/usr/local/nginx/conf/htpasswd;
//指定用户密码文件
}
#yum install -y httpd

//安装 httpd,也可以使用之前编译安装的 Apache2.4
下面创建和更新用于基本认证的用户认证密码文件

#htpasswd -c /usr/local/nginx/conf/htpasswd dongying
new password:
re-type new password:
Adding pass word for user dongying
# /usr/local/nginx/sbin/nginx -t
#/usr/local/nginx/sbin/nginx-sreload
#mkdir /data/nginx/test.com
#echo "test.com" >/data/nginx/test.com/index.html

测试检测

#curl-I-x127.0.0.1:80test.com

状态码401

#curl-udongying:000000-x127.0.0.1:80test.com

测试成功

[root@localhostvhost]#systemctlstopfirewalld
[root@localhostvhost]#setenforce0

编辑C:\Windows\System32\drivers\etc\hosts文件,添加映射
IPtest.com
打开浏览器访问test.com

域名重定向

Nginx和httpd的域名重定向和httpd的类似。

配置虚拟主机文件:

# vim test.com.conf

server
{
   listen 80;
   server_name test.com test1.com test2.com;				#Nginx中,server_name 后面可以跟多个域名
   index index.html index.htm index.php;
   root /data/nginx/test.com;
   if ($host != 'test.com')
   {
   rewrite ^/(.*)$ http://test.com/$1 permanent;				#permanent为永久重定向,相当于httpd的R=301;还有个redirect,为临时重定向,相当于R=302
   }  
} 

验证上面配置:

# /usr/local/nginx/sbin/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

# /usr/local/nginx/sbin/nginx -s reload

# curl -x127.0.0.1:80 test1.com/123.txt -I
HTTP/1.1 301 Moved Permanently				#301 永久moved
Server: nginx/1.12.1
Date: Thu, 05 Jul 2018 08:41:51 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/123.txt

Nginx的访问日志

先查看一下Nginx的日志格式:

# grep -A2 log_format /usr/local/nginx/conf/nginx.conf
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
和httpd类似,也是在主配置文件中定义的日志格式
combined_realip		日志格式的名字,后面可以调用它;    
$remote_addr		访问网站的用户的出口ip;  
$http_x_forwarded_for		代理服务器的ip,如果使用了代理则会记录代理的ip;  
$time_local		当前的时间;  
$host		访问的主机名;  
$request_uri		访问的URL地址;  
$status		状态码;  
$http_referer		referer地址;    
$http_user_agent		user_agent。  
指定访问日志的路径:
# cd /usr/local/nginx/conf/vhost/

# vim test.com.conf 

server
{
   listen 80;
   server_name test.com test1.com test2.com;
   index index.html index.htm index.php;
   root /data/nginx/test.com;
   if ($host != 'test.com')
   {
   rewrite ^/(.*)$ http://test.com/$1 permanent;
   }
   access_log /tmp/1.log combined_realip;				#使用access_log来指定日志的存储路径,最后面指定日志的格式名字
}
...
验证上面配置:

```puppet
# /usr/local/nginx/sbin/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

# /usr/local/nginx/sbin/nginx -s reload

# curl -x127.0.0.1:80 test.com/111
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>

# cat /tmp/1.log 
127.0.0.1 - [06/Jul/2018:11:15:12 +0800] test.com "/111" 404 "-" "curl/7.29.0"				#curl访问记录
192.168.33.1 - [06/Jul/2018:11:18:35 +0800] test.com "/" 200 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"				#Windows上面浏览器访问记录
192.168.33.1 - [06/Jul/2018:11:18:35 +0800] test.com "/favicon.ico" 404 "http://test.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
192.168.33.1 - [06/Jul/2018:11:18:41 +0800] test.com "/111" 404 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"

Nginx的日志比较简单,但没有像httpd那样自带的切割工具,要想切割Ngin日志需要借助系统的切割工具或自定义脚本。

这里我们自定义一个日志切割脚本:

# vim /usr/local/sbin/nginx_log_rotate.sh	
#! /bin/bash
d= `data -d "-1 day" +%Y%m%d`
logdir="/data/logs"				#假设Nginx的日志存放路径为/data/logs
nginx_pid="/usr/local/nginx/logs/nginx.log"
cd $logdir
for log in `ls *.log`
do
  mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid

写完脚本之后,还需要增加任务计划:

0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值