Lnmp环境搭建及配置

一.lnmp简介

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统

二.Mysql安装

 Mysql包下载

          路径:  https://www.mysql.com/下载源码包:

   下载mysql-5.6.45-linux-glibc2.12-x86_64.tar(存放在/usr/local/src 中)

# cd /usr/local/src/
#  yum install -y vim wget      //下载需要用到的工具包
# tar -zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
# mv mysql-5.6.43-linux-glibc2.12-x86_64 /usr/local/mysql
安装和配置:
# cd /usr/local/mysql
# useradd -s /sbin/nologin mysql   //建立MySQL用户
# mkdir -p /data/mysql     //创建datadir,存放数据库文件
# chown -R mysql:mysql /data/mysql   // 更改权限
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql            //定义运行身份和数据库的安装目录
需安装相关编译包
# yum install -y perl-Module-Install
# cp support-files/my-default.cnf /etc/my.cnf   //复制配置文件
# vim /etc/my.cnf       //修改配置文件,修改以下内容
basedir = /usr/local/mysql            //MYSQL包所在路径
datadir = /data/mysql                 //存放数据库位置
port = 3306                    //定义MYSQL服务监听的端口号
server_id = 155                  //定义MYSQL服务的ID号
socket = /tmp/mysql.sock          //定义MySQL服务监听的套接字地址
# cp  support-files/mysql.server  /etc/init.d/mysqld     //复制启动脚本文件
# vim /etc/init.d/mysqld    //修改启动脚本内容如下
basedir=/usr/local/mysql              //MYSQL包所在路径  
datadir=/data/mysql                  //存放数据库位置
# chkconfig --add mysqld         //把mysql服务加到系统服务列表中
# chkconfig mysqld on          //设置开机自启
# service mysqld start           //启动服务

       查看否启动:

     # ps aux | grep mysqld   

   三.PHP安装

 PHP下载

     地址为:PHP: Manual Quick Reference  

​
# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
# tar -zxvf php-5.6.30.tar.gz
# useradd -s /sbin/nologin php-fpm
# cd php-5.6.30
  编译安装
# ./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 ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel  libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-deve libcurl-devel
# yum install -y libmcrypt-develvi
(注意 epel-release  libmcrypt-devel两个不能一起安装,因为CentOs6默认的yum源没有 libmcrypt-devel这个包,只能借助epel的yum源,所以先安装epel,再安装libmcrypt)
# make -j4 &&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/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod 755 /etc/init.d/php-fpm
# useradd -s /sbin/nologin php-fpm
useradd: user 'php-fpm' already exists
# service php-fpm start
# chkconfig php-fpm on
# ps aux | grep php-fpm
​

 三.Nginx安装

1.nginx简介

     Nginx (engine x)是一个开源、高性能的 HTTP 和反向代理 Web 服务,同时也提供了 IMAP/POP3/SMTP 服务

使用场景:

HTTP 服务器,反向代理,负载均衡,第三方插件

  1. Nginx下载及安装

  nginx包下载地址:nginx news

# tar -zxvf nginx-1.18.0.tar.gz
# cd nginx-1.18.0
# ./configure --prefix=/usr/local/nginx
# make -j4 && make install
# 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
# 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.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
# ps aux | grep nginx    
测试PHP解析
# vim /usr/local/nginx/html/2.php

# curl localhost/2.php

.Nginx 配置

 1.默认虚拟主机

 # vim /usr/local/nginx/conf/nginx.conf
修改主配置文件nginx.cnf,在结束符号}上面加入“ include vhost/*.cnf; ”,
# mkdir /usr/local/nginx/conf/vhost
# cd /usr/local/nginx/conf/vhost
#  vim default.conf
             内容如下:
server
{                    listen 80 default_server; 
                server_name 123.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
# curl -x127.0.0.1:80 6666.com           
用户认证
      # vim test.com.conf
server
{
   listen 80;
   server_name test.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;
   }
}
#  /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# yum install -y httpd
#  htpasswd -c /usr/local/nginx/conf/htpasswd lfb
#  /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
#  mkdir /data/nginx/test.com
# echo "test" > /data/nginx/test.com/index.html
# curl -x127.0.0.1:80 test.com -I # vim /usr/local/nginx/conf/nginx.conf
修改主配置文件nginx.cnf,在结束符号}上面加入“ include vhost/*.cnf; ”,
# mkdir /usr/local/nginx/conf/vhost
# cd /usr/local/nginx/conf/vhost
#  vim default.conf
             内容如下:
server
{                    listen 80 default_server; 
                server_name 123.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
# curl -x127.0.0.1:80 6666.com

 用户认证

# vim test.com.conf
server
{
   listen 80;
   server_name test.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;
   }
}
#  /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# yum install -y httpd
#  htpasswd -c /usr/local/nginx/conf/htpasswd lfb
#  /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
#  mkdir /data/nginx/test.com
# echo "test" > /data/nginx/test.com/index.html
# curl -x127.0.0.1:80 test.com -I

配置验证

配置完可在Windows上访问地址test.com,文件在C:\Windows\System32\drivers\etc\hosts。打开文件在最后一行输入:

192.168.131.131 test.com

然后,去浏览器访问会有弹窗出现。

输入用户名以及密码,就可以正常访问

# curl -x127.0.0.1:80 -u lfb:000000 test.com -I

 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;
#   location /
#   {
#      auth_basic  "Auth";
#      auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
#   } 
  if ($host != 'test.com')
   {   rewrite ^/(.*)$ http://test.com/$1 permanent; 
  }
}
#  /usr/local/nginx/sbin/nginx -t
#  /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 test1.com/123.txt -I

4.Nginx的访问日志

查看Nginx日志格式

# grep -A2 log_format /usr/local/nginx/conf/nginx.conf
和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。 
到虚拟主机配置文件中指定访问日志的路径
#  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;
#   location /
#   {
#      auth_basic  "Auth";
#      auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
#   }
   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
# cat /tmp/1.log

5.日志切割脚本

# vim /usr/local/sbin/nginx_log_rotate.sh

6.配置静态文件不记录日志并添加过期时间

# >test.com.conf
# 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;
   }
   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
# echo "111" > /data/nginx/test.com/1.js
# echo "222" > /data/nginx/test.com//2.jpg
# touch /data/nginx/test.com/1.jss
# curl -x127.0.0.1:80 test.com/1.js -I
#  curl -x127.0.0.1:80 test.com/2.jpg -I
# curl -x127.0.0.1:80 test.com/1.jss -I
# cat /tmp/1.log

Nginx防盗链
# >test.com.conf
# 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;
   }
   location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
   {
      expires  7d;
      valid_referers none blocked server_names *.test.com;
      if ($invalid_referer)
     {
      return 403;
     }
      access_log off;
   }
   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 -I -e "http://aaa.com/1.txt" test.com/2.jpg
#  curl -x127.0.0.1:80 -I -e "http://test.com/1.txt" test.com/2.jpg
访问控制
# vim test.com.conf
添加内容如下
        location /admin/

{
      allow 192.168.131.131;
      allow 127.0.0.1;
      deny all;
}
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# mkdir /data/nginx/test.com/lfb/
# echo "123" > /data/nginx/test.com/lfb/1.html
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 test.com/lfb/1.html
Nginx解析PHP
# >test.com.conf
# 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;
        }
     location ~ \.php$
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/phpfcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /data/nginx/test.com$fastcgi_script_name;
        }
    access_log /tmp/1.log combined_realip;
}
# /usr/local/nginx/sbin/nginx -t
#  /usr/local/nginx/sbin/nginx -s reload
nginx代理
# vim proxy.conf
  server
{
    listen 80;
    server_name www.baidu.com;
    location /
       {
          proxy_pass       http://220.181.38.150;
          proxy_set_header Host    $host;
          proxy_set_header X-Real-IP    $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
}

# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 www.baidu.com -I

你可能感兴趣的:(mysql,数据库,linux,lnmp)