本文中LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。     Linux 是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。 代表版本有:debian、centos、ubuntu、fedora、gentoo等。     Nginx 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。     Mysql 是一个小型关系型数据库管理系统。     PHP 是一种在服务器端执行的嵌入HTML文档的脚本语言。 这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。

 

生产环境:  (单击下面的服务名称即可下载)
操作系统:CentOS-6.6-64位最小化安装
软件版本:nginx-1.9.4(提供web服务)   mysql-5.6.26(提供数据库服务)    php-5.5.28(提供PHP运行环境)
约定:所有软件均安装在 /app/ 下,mysql数据文件 /data/mysql_db ,web目录 /data/web/

 

1、编译安装Mysql

# yum -y install ncurses* cmake bison* openssl* gcc gcc-c++
安装编译mysql时需要的依赖包

# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.26.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.26.tar.gz && cd mysql-5.6.26
解压mysql包

# cmake . -DCMAKE_INSTALL_PREFIX=/app/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成功
Screenshot-5
# make && make install
执行编译安装
Screenshot-6
配置Mysql:

# vim /etc/my.cnf
新建mysql配置文件,增加下面配置项

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

[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 = /app/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

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

# /app/mysql/scripts/mysql_install_db \
--user=mysql \
--datadir=/data/mysql_db \
--basedir=/app/mysql
初始化mysql系统数据库

# cp /app/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
# vim /etc/rc.d/init.d/mysqld  修改下面两项目录配置
    basedir=/app/mysql   datadir=/data/mysql_db
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig mysqld on
把mysql加入系统服务,赋予执行权限,并设置为开机启动

# service mysqld start
启动mysql

Screenshot-7

# ln -s /app/mysql/bin/* /usr/bin
将mysql所有命令链接到系统环境变量中

命令行输入mysql ,进入mysql数据库
Screenshot-8

Mysql配置结束

 

 

2、编译安装Nginx

# yum -y install zlib* pcre* lua-devel
安装编译Nginx时需要的依赖包

# wget http://mirrors.sohu.com/nginx/nginx-1.9.4.tar.gz
下载Nginx安装包

# groupadd www && useradd -g www www -s /sbin/nologin
创建Nginx组和用户,设置不允许登陆系统

# tar zxvf nginx-1.9.4.tar.gz  && cd nginx-1.9.4
解压Nginx包

./configure --prefix=/app/nginx \
--sbin-path=/app/nginx/sbin/nginx \
--conf-path=/app/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通过

Screenshot-9

# make && make install 
编译安装Nginx

# mkdir -p /tmp/nginx/client-body-temp
创建缓存目录

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

Screenshot-11

# 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: /app/nginx/logs/nginx.pid
# config: /app/nginx/conf/nginx.conf
nginxd=/app/nginx/sbin/nginx
nginx_config=/app/nginx/conf/nginx.conf
nginx_pid=/app/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 /app/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

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

# chmod 755 /etc/rc.d/init.d/nginx
# chkconfig nginx on
赋予Nginx执行权限,并设置为开机启动

Screenshot-12

Nginx配置结束

 

3、编译安装PHP

# yum -y install gd-devel curl-devel libxml2-devel bzip2-devel \
libxpm-devel libXpm mbstring exif libicu-devel libmcrypt-devel \
php-mcrypt bzip2* libjpeg* libpng* freetype*
安装编译PHP时需要的依赖包

# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
下载PHP依赖的libmcrypt加密算法扩展库

# wget http://mirrors.sohu.com/php/php-5.5.28.tar.gz
下载PHP安装包

# tar zxvf libmcrypt-2.5.7.tar.gz && cd libmcrypt-2.5.7
解压libmcrypt包

# ./configure && make && make install
编译安装libmcrypt

# tar zxvf php-5.5.28.tar.gz && cd php-5.5.28
解压PHP包

./configure \
--prefix=/app/php5.5 \
--sysconfdir=/app/php5.5/etc \
--with-config-file-path=/app/php5.5/etc \
--with-config-file-scan-dir=/app/php5.5/etc/conf.d \
--enable-maintainer-zts \
--enable-phpdbg \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-zend-vm=GOTO \
--enable-inline-optimization \
--disable-debug \
--disable-short-tags \
--without-pear \
--enable-bcmath \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql \
--without-pdo-sqlite \
--disable-ipv6 \
--with-curl \
--enable-ftp \
--enable-sockets \
--with-openssl \
--with-bz2 \
--with-zlib \
--with-zlib-dir \
--enable-zip \
--enable-json \
--with-iconv \
--with-iconv-dir \
--with-pcre-regex \
--with-pcre-dir \
--enable-mbstring \
--enable-mbregex \
--with-gettext \
--with-mcrypt \
--with-mhash \
--disable-calendar \
--enable-gd-jis-conv \
--with-gd \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--enable-gd-native-ttf \
--enable-exif \
--enable-xml \
--with-libxml-dir \
--enable-soap \
--disable-xmlwriter \
--disable-xmlreader \
--enable-sysvsem \
--enable-sysvmsg \
--enable-shmop \
--enable-sysvshm \
--enable-pcntl \
--disable-flatfile \
--enable-fd-setsize=4096 \
--enable-intl \
--with-xpm-dir

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

Screenshot-13

# make && make install
编译安装PHP

配置PHP

# vim /app/php5.5/etc/php.ini
创建php.ini 配置文件,并写入以下内容

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

[PHP]
engine = On
short_open_tag = ON
asp_tags = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 17
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,proc_close,
proc_get_status,proc_nice,proc_terminate,php_uname,posix_getpwuid,posix_kill,
posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,posix_setuid,posix_uname,
syslog,parse_ini_file,eval,apache_child_terminate,apache_setenv,
define_syslog_variables,escapeshellarg,escapeshellcmd,show_source
disable_classes =
zend.enable_gc = On
expose_php = OFF
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL & ~E_ NOTICE & ~E_STRICT
display_errors = off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
error_log=/data/logs/php/php-error.log
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = PRC
[filter]
[iconv]
[intl]
[sqlite]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatibility_mode = Off
mssql.secure_connection = Off
[Assertion]
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]

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

# vim /app/php5.5/etc/php-fpm.conf
新建php-fpm配置文件,并写入以下内容

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

[global]
pid = /app/php5.5/bin/php-fpm.pid
log_level = notice
error_log = /data/logs/php/php-error.log

[www]
catch_workers_output = yes
user = www
group = www
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pm.max_requests = 512
request_slowlog_timeout = 8s
slowlog = /data/logs/php/php5.5-slow.log

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

# mkdir -p /data/logs/php
创建php日志存放目录

# /app/php5.5/sbin/php-fpm
启动php

PHP配置结束

 

4、配置nginx支持php

# vim /app/nginx/conf/nginx.conf
修改nginx配置文件,将一下行写入到nginx.conf

=======================================
user www;
worker_processes auto;
worker_rlimit_nofile 51200;
error_log /data/logs/nginx/error.log crit;
pid /app/nginx/logs/nginx.pid;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 64k;
client_max_body_size 8m;
sendfile on;
server_tokens off;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 256k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 4;
gzip_types text/plain application/javascript application/x-javascript text/css;
gzip_vary on;
gzip_disable "MSIE [1-6]."
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for';
include host/*.host;
}
=======================================

# mkdir /app/nginx/conf/host
创建网站server文件存放目录

# vim /app/nginx/conf/host/local.host
创建server文件

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

server {
listen 80;
server_name 127.0.0.1;
index index.shtml index.html index.php;
root /data/web/;
access_log /data/logs/nginx/www.access.log;
location ~ .*\.php$ {
include fastcgi-php5.5.conf;
}
}

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

# vim /app/nginx/conf/fastcgi-php5.5.conf
创建fastcgi配置文件

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

fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
client_max_body_size 8m;
client_body_buffer_size 128k;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REDIRECT_STATUS 200;

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

# mkdir /data/web
创建网站根目录

# echo "<?phpinfo()?>" >> /data/web/index.php
创建phpinfo 测试文件
重新启动nginx,访问web服务器

至此LNMP环境安装完成

Screenshot-14

LNMP一键安装包:http://yunpan.cn/cmcwDgujq6g5G (提取码:5418)

欢迎在下方对本文提出意见或建议,小白会第一时间回复您。

 

欢迎留言