LNMP环境搭建以及配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP环境搭建以及配置相关的知识,希望对你有一定的参考价值。

LNMP是一个基于linux系统编写的nginxphpmysql环境。可以在VPS、独立主机上轻松的安装LNMP生产环境。

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。

Nginx是一个小巧而高效的Linux下的Web服务器软件,Nginx性能稳定、功能丰富、运维简单、处理静态文件速度快且消耗系统资源极少.

操作系统是基于CentOS-6.6-64位的LINUX系统,安装软件的版本如下:nginx-1.6.2,mysql-5.6.17,php-5.5.16

在此说明一下:所有软件均安装在 /lnmp/ 下,mysql数据文件 /data/mysql_db ,站点总目录 /data/web/

下面是具体安装LNMP的详细步骤

一、编译安装Mysql

//安装编译mysql时需要的依赖包

# yum -y install ncurses* cmake bison* openssl* gcc gcc-c++

//下载mysql安装包

# wget http://mirrors.sohu.com/mysql/soft/mysql-5.6.17.tar.gz

//创建Mysql组和用户 设置不允许登陆系统

# groupadd mysql && useradd -g mysql mysql -s /sbin/nologin

//建立mysql数据库存放目录

# mkdir -p /data/mysql_db

//设置mysql数据库目录权限

# chown -R mysql:mysql /data/mysql_db

//解压mysql包

# tar zxvf mysql-5.6.17.tar.gz && cd mysql-5.6.17

# cmake . -DCMAKE_INSTALL_PREFIX=/lnmp/mysql \

 -DMYSQL_DATADIR=/data/mysql_db \

 -DSYSCONFDIR=/etc \

 -DWITH_MYISAM_STORAGE_ENGINE=1 \

 -DWITH_INNOBASE_STORAGE_ENGINE=1 \

 -DWITH_MEMORY_STORAGE_ENGINE=1 \

 -DWITH_READLINE=1 \

 -DMYSQL_UNIX_ADDR=/data/mysql_db/mysql.sock \

 -DMYSQL_TCP_PORT=3306 \

 -DENABLED_LOCAL_INFILE=1 \

 -DWITH_PARTITION_STORAGE_ENGINE=1 \

 -DEXTRA_CHARSETS=all \

 -DDEFAULT_CHARSET=utf8 \

 -DDEFAULT_COLLATION=utf8_general_ci \

 -DWITH_FEDERATED_STORAGE_ENGINE=1 \

 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \

 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

 -DWITH_SSL=yes

执行Cmake编译Mysql指定安装目录,数据存放目录,配置文件存放目录,出现如下语句说明cmake成功

技术分享

//执行编译安装

# make && make install

配置Mysql:

//新建mysql配置文件,增加下面配置项

# vim /etc/my.cnf

===============================

[client]

 port = 3306

 socket = /data/mysql_db/mysql.sock

 [mysql]

 no-auto-rehash

 [mysqld]

 user = mysql

 port = 3306

 socket = /data/mysql_db/mysql.sock

 pid-file=/data/mysql_db/mysql.pid

 basedir = /lnmp/mysql/

 datadir = /data/mysql_db

 open_files_limit = 51200

 back_log = 600

 table_open_cache = 4096

 max_connections = 3000

 max_connect_errors = 6000

 external-locking = FALSE

 max_allowed_packet = 32M

 sort_buffer_size = 16M

 join_buffer_size = 16M

 thread_cache_size = 300

 thread_concurrency = 8

 query_cache_size = 128M

 query_cache_limit = 4M

 thread_stack = 512K

 transaction_isolation = READ-COMMITTED

 tmp_table_size = 256M

 max_heap_table_size = 256M

 long_query_time = 10

 slow_query_log = 5

 slow_query_log_file = /data/log/mysql_slow-log.log

 log_queries_not_using_indexes=1

 log_bin=/data/mysql_db/mysql-binlog

 binlog_format=mixed

 binlog_cache_size = 4M

 max_binlog_cache_size = 8M

 max_binlog_size = 512M

 expire_logs_days = 7

 key_buffer_size = 32M

 read_buffer_size = 8M

 read_rnd_buffer_size = 16M

 bulk_insert_buffer_size = 64M

 myisam_sort_buffer_size = 128M

 myisam_max_sort_file_size = 10G

 myisam_repair_threads = 1

 myisam_recover

 lower_case_table_names = 1

 skip-name-resolve

 slave-skip-errors = 1032,1062

 replicate-ignore-db=mysql

 server-id = 1

 innodb_additional_mem_pool_size = 16M

 innodb_buffer_pool_size = 2048M

 innodb_data_file_path = ibdata1:1024M:autoextend

 innodb_file_io_threads = 4

 innodb_thread_concurrency = 8

 innodb_flush_log_at_trx_commit = 2

 innodb_log_buffer_size = 16M

 innodb_log_file_size = 128M

 innodb_log_files_in_group = 3

 innodb_max_dirty_pages_pct = 90

 innodb_lock_wait_timeout = 120

 innodb_file_per_table = 0

 [mysqldump]

 quick

 max_allowed_packet = 32M

 [mysqld_safe]

 open-files-limit = 8192

 log-error=/data/mysql_db/mysql.err

===============================

//初始化mysql系统数据库

# /lnmp/mysql/scripts/mysql_install_db \

 --user=mysql \

 --datedir=/data/mysql_db \

 --basedir=/lnmp/mysql

//把mysql加入系统服务,赋予执行权限,并设置为开机启动

# cp /lnmp/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld

 # vim /etc/rc.d/init.d/mysqld  修改下面两项目录配置

    basedir=/lnmp/mysql   datadir=/data/mysql_db

 # chmod 755 /etc/rc.d/init.d/mysqld

 # chkconfig mysqld on

//启动mysql

# service mysqld start

技术分享

//将mysql所有命令链接到系统环境变量中

# ln -s /lnmp/mysql/bin/* /usr/bin

//命令行输入mysql ,进入mysql数据库

技术分享

Mysql配置结束

二、编译安装Nginx

//安装编译Nginx时需要的依赖包

# yum -y install zlib* pcre* lua-devel

//下载Nginx安装包

# wget http://www.jiangzunshao.com/soft/nginx-1.6.2.tar.gz

//创建Nginx组和用户,设置不允许登陆系统

# groupadd www && useradd -g www www -s /sbin/nologin

//解压Nginx包

# tar zxvf nginx-1.6.2.tar.gz  && cd nginx-1.6.2

./configure --prefix=/lnmp/nginx \

 --sbin-path=/lnmp/nginx/sbin/nginx \

 --conf-path=/lnmp/nginx/conf/nginx.conf \

 --user=www \

 --group=www \

 --with-pcre \

 --with-file-aio \

 --with-poll_module \

 --with-http_ssl_module \

 --with-http_realip_module \

 --with-http_stub_status_module \

 --with-http_sub_module \

 --with-http_gzip_static_module \

 --with-http_dav_module \

 --with-http_flv_module \

 --with-http_mp4_module \

 --with-http_spdy_module \

 --with-http_sub_module \

 --with-http_dav_module \

 --without-http_uwsgi_module \

 --without-http_scgi_module \

 --http-log-path=/data/logs/nginx/access.log \

 --error-log-path=/data/logs/nginx/error.log \

 --http-client-body-temp-path=/tmp/nginx/client-body-temp \

 --http-proxy-temp-path=/tmp/nginx/proxy-temp \

 --http-fastcgi-temp-path=/tmp/nginx/fastcgi-temp

执行./coonfigure对nginx进行配置以及检测当前环境是否满足安装需求,出现下图说明./configure通过

技术分享

/编译安装Nginx

# make && make install 

//创建缓存目录

# mkdir -p /tmp/nginx/client-body-temp

# /lnmp/nginx/sbin/nginx 

启动nginx,浏览器访问web服务器ip 看是否启动成功。成功界面如下:

技术分享

# vim /etc/rc.d/init.d/nginx

为nginx提供开机启动脚本,复制以下内容到服务文件中

===============================

#!/bin/bash

 # nginx Startup script for the Nginx HTTP Server

 # it is v.0.0.2 version.

 # chkconfig: - 85 15

 # description: Nginx is a high-performance web and proxy server.

 # It has a lot of features, but it‘s not for everyone.

 # processname: nginx

 # pidfile: /lnmp/nginx/logs/nginx.pid

 # config: /lnmp/nginx/conf/nginx.conf

 nginxd=/lnmp/nginx/sbin/nginx

 nginx_config=/lnmp/nginx/conf/nginx.conf

 nginx_pid=/lnmp/nginx/logs/nginx.pid

 RETVAL=0

 prog="nginx"

 # Source function library.

 . /etc/rc.d/init.d/functions

 # Source networking configuration.

 . /etc/sysconfig/network

 # Check that networking is up.

 [ ${NETWORKING} = "no" ] && exit 0

 [ -x $nginxd ] || exit 0

 # Start nginx daemons functions.

 start() {

 if [ -e $nginx_pid ];then

 echo "nginx already running...."

 exit 1

 fi

 echo -n $"Starting $prog: "

 daemon $nginxd -c ${nginx_config}

 RETVAL=$?

 echo

 [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

 return $RETVAL

 }

 # Stop nginx daemons functions.

 stop() {

 echo -n $"Stopping $prog: "

 killproc $nginxd

 RETVAL=$?

 echo#

 [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /lnmp/nginx/logs/nginx.pid

 }

 reload() {

 echo -n $"Reloading $prog: "

 #kill -HUP `cat ${nginx_pid}`

 killproc $nginxd -HUP

 RETVAL=$?

 echo

 }

 test() {

 $nginxd -t

 }

 # See how we were called.

 case "$1" in

 test)

 test

 ;;

 start)

 start

 ;;

 stop)

 stop

 ;;

 reload)

 reload

 ;;

 restart)

 stop

 start

 ;;

 status)

 status $prog

 RETVAL=$?

 ;;

 *)

 echo $"Usage: $prog {start|stop|restart|reload|status|help|test}"

 exit 1

 esac

 exit $RETVAL

===============================

//赋予Nginx执行权限,并设置为开机启动

 # chmod 755 /etc/rc.d/init.d/nginx

 # chkconfig nginx on

技术分享

Nginx配置结束

以上是关于LNMP环境搭建以及配置的主要内容,如果未能解决你的问题,请参考以下文章

LNMP架构搭建Discuz论坛(实战!)

LNMP架构搭建(一键部署)

LNMP搭建过程详解,验证搭建论坛

lnmp环境搭建-手动部署详细文档

使用编译搭建LNMP环境

Centos7构建LNMP平台