2020.1.2(搭建lnmp)
1.1安装mysql
1.1.1下载
wget mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
1.1.2解压
tar -zvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
1.1.3安装和配置
安装前期准备
[ -d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_old
mv mysql**** /usr/local/mysql //移动位置
cd /usr/local/mysql
mkdir -p /data/mysql //数据库存放的位置
chown -R mysql:mysql /data/mysql //更改权限
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
#make &&make install //编译安装
配置MySQL
cp sopport-files/my-default.cnf /etc/my.cnf //备份,避免配置出现致命错误
vim /etc/my.cnf //配置文件
#cp support-files/mysql.server /etc/init.d/mysql //复制启动脚本
#chmod 755 /etc/init.d/mysql //修改属性为755
#vim /etc/init.d/mysql //修改启动脚本 (修改地方datadir=/data/mysql)//数据库存放目录
#chkconfig --add mysql //把mysql服务加入到系统服务列表
#chkconfig mysql on // 使其开机就启动
#service mysql start // 启动服务
如果启动不了请到/data/mysql/目录下查看错误日志,名通常是主机名.err。检查MySQL是否启动的命令
#ps aus |grep mysql //结果应大于2行
#netstat -lnp |grep 3306 //查看有没有监听3306端口
(1)遇到错误信息“FATAL ERROR”:...(原因:缺少包prel-moudule-install )
解决方法使用命令yum install -y prel-moudule-install
1.2安装PHP
cd /usr/local/src
wget ...
tar -xzvf .....
useradd -s /sbin/nologin php-fpm
cd php-5.6
./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usrlocal/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-gd --with-jpeg-dir --with-png-dir --withfreetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl
346 ./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-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 --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl
错误信息:yum install -y libcurl-devel
make &&make install
修改配置文件
cp php.ini-production /usr/local/php-fpm/etc/php.ini
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]
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
/usr/local/php-fpm/sbin/php-fpm -t
cp /usr/local/src/php-5.6.36/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 on //打开php-fpm服务
chkconfig php-fpm on //设置开机自启
ps aux |grep php-fpm //检测php-fpm是否启动
1.3安装nginx
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar -xzvf nginx-1.14.2.tar.gz
cd nginx-1.14.2/
./configure --prefix=/usr/local/nginx
make && make install
编写nginx启动脚本,并加入系统服务
vim /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
return 0
}
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 --add nginx
设置开机启动nginx
chkconfig nginx on
更改nginx的配置文件
> /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.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/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_buffers 4 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_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
/usr/local/nginx/sbin/nginx -t //检测文件
service nginx start //启动nginx 服务
ps aux |grep nginx //检测是否启动
测试是否正确解析php
vim /usr/local/nginx/html/2.php //创建测试文件
内容:
<?php
echo "test php scripts"
?>
curl localhost/2.php
解析正常
1.4nginx配置
1.4.1默认虚拟主机
修改朱配置文件
include vhost/*.conf; //意思是/usr/local/nginx/conf/vhost/下面的所有以.conf结尾的文件都会加载
(把所有的虚拟主机配置文件放到vhost目录下面)
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 //检测配置文件是否正确
/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
1.4.2用户认证
cd /usr/local/nginx/conf/vhost //进入目录
service{
listen 80;
server_name test.com test1.com test2.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;
}
}
htpasswd -c /usr/local/nginx/conf/htpasswd admin //创建admin用户
密码:
确认密码;
/usr/local/nginx/sbin/nginx -t //测试文件是否正确
/usr/local/nginx/sbin/nginx -s reload //重新加载服务
mkdir /data/nginx/test.com
echo "test.com" > /data/nginx/test.com/index.html //加入内容
curl -I -x127.0.0.1:80 test.com
在主机上host文件加入192.168.41.110 test.com
1.4.3域名重定向
配置文件: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;
}
}
/usr/local/nginx/sbin/nginx -t //测试文件
/usr/local/nginx/sbin/nginx -s reload //重新加载服务
url -x127.0.0.1:80 test1.com/123.txt -I
1.4.4nginx的访问日志
查看nginx的日志格式
grep -A2 log_format /usr/local/nginx/conf/nginx.conf
配置文件:
{
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;
}
/usr/local/nginx/sbin/nginx -t //测试文件
/usr/local/nginx/sbin/nginx -s reload //重新加载服务
curl -x127.0.0.1:80 test.com/111
nginx 的日志切割,切割nginx日志需要借助系统的切割工具后者自定义脚本。(以下是日志切割脚本)
vim /usr/local/sbin/nginx_log_rotate.sh
配置内容:
#! /bin/hash
d=`data -d "-1 day" +%Y%m%d`
logdir="/data/logs"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
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
1.4.5配置静态文件不记录日志并添加过期时间
配置文件:
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;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
access_log /tmp/1.log combined_realip;
}
/usr/local/nginx/sbin/nginx -t //测试文件
/usr/local/nginx/sbin/nginx -s reload //重新加载服务
echo "1111111" > /data/nginx/test.com/1.js //创建js文件
echo "2222222" > /data/nginx/test.com/1.jpg //创建jpg文件
touch /data/nginx/test.com/1.js //创建一个对比文件
测试:curl
cat /tmp/1.log