微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

LNMP环境的搭建配置

LNMP环境的搭建配置

       概述:Linux + Nginx + MysqL + PHPPHP是一种脚本语言,当前中国乃至世界上使用PHP语言开发网站非常普遍。Nginx一个web服务软件,和apache是一类软件,目前使用Nginx的企业越来越多,MysqL是当前最为流行的小型关系型数据库,LNMP就是一个支持解析PHP程序的环境。

一,安装MysqL

MysqL初始化

# cd /usr/local/src

# wget http://mirrors.163.com/MysqL/Downloads/MysqL-5.7/MysqL-5.7.31-linux-glibc2.12-x86_64.tar.gz

1,解压MysqL:tar zxvf MysqL-5.7.31-linux-glibc2.12-x86_64.tar.gz

2,更改位置:# mv MysqL-5.7.31-linux-glibc2.12-x86_64 /usr/local/MysqL

3,创建MysqL用户:# useradd -s /sbin/nologin MysqL

4,创建datadir,MysqL数据会放在这里:#cd /usr/local/MysqL

# mkdir -p /data/MysqL

5,更改权限:# chown -R MysqL:MysqL /data/MysqL

6,指定MysqL用户MysqL包所在的位置,datadir数据库的安装位置:# ./bin/MysqLd/ --initialize --user=MysqL --basedir=/usr/local/MysqL --datadir=/usr/local/MysqL/data/MysqL

配置MysqL

1,配置/etc/my.cnf文件

查看/usr/local/MysqL/support-files下有没有my-default.cnf文件

若有,则复制 #cp /etc/my.cnfcp /usr/local/MysqL/support-files/my-default.cnf /etc/my.cnf

若没有,则vim /etc/my.cnf,编辑

# For advice on how to change settings please see

# http://dev.MysqL.com/doc/refman/5.7/en/server-configuration-defaults.html

# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the

# *** default location during install, and will be replaced if you

# *** upgrade to a newer version of MysqL.

[MysqLd]

# Remove leading # and set to the amount of RAM for the most important data

# cache in MysqL. Start at 70% of total RAM for dedicated server, else 10%.

innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging

# changes to the binary log between backups.

# log_bin

# These are commonly set, remove the # and set as required.

basedir = /usr/local/MysqL

datadir = /usr/local/MysqL/data/MysqL

port = 3306

server_id = 128

socket = /tmp/MysqL.sock

# Remove leading # to set options mainly useful for reporting servers.

# The server defaults are faster for transactions and fast SELECTs.

# Adjust sizes as needed, experiment to find the optimal values.

join_buffer_size = 128M

sort_buffer_size = 2M

read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

2,复制启动脚本文件修改属性

# cp support-files/MysqL.server /etc/init.d/MysqLd

#  chmod 755 /etc/init.d/MysqLd

3,修改启动脚本/etc/init.d/MysqLd中datadir(数据库文件储存的地方)的数据

# vim /etc/init.d/MysqLd

修改:datadir=/data/MysqL

4,把启动脚本加入系统服务项,设定开机启动

# chkconfig --add MysqLd

# chkconfig MysqLd on

5,启动MysqL

# service MysqLd start

6,检查MysqL是否启动

# ps aux |grep MysqLd  //结果应该大于两行

# netstat -lnp|grep 3306  //看看有没有监听3306端口

安装PHP

1,针对NginxPHP安装和针对apache的PHP安装是有区别的 

#cd /usr/local/src

#wget http://cn2.PHP.net/distributions/PHP-5.6.39.tar.bz2

2,解压源码包#  tar zxf -xjf PHP-5.6.39.tar.bz2 

3,创建PHP-fpm账户用来运行PHP-fpm服务。PHP一个服务PHP-fpm的形式出现,独立存在于linux系统中,方便管理:  #useradd -s /sbin/nologin PHP-fpm 

4,配置编译选项

#cd PHP-5.3.27 

# ./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

5,安装库

1) yum install -y libxml2-devel

2) yum install -y openssl openssl-devel

3)  yum install -y bzip2 bzip2-devel

4)  yum install -y libpng libpng-devel

5)  yum install -y freetype freetype-devel

6) yum install -y epel-release

7)  yum install -y libmcrypt-devel

8)  yum -y install libjpeg-devel

注:可能会出现的问题:1,Linux执行./configuer 编译命令报configure: WARNING: unrecognized options: ....

checking for cc… no

checking for gcc… no

解决办法:原因是缺少gcc 编译环境

有yum源的直接执行 # yum -y install gcc gcc-c++

2,编译命令报checking libxml2 install dir... no

checking for xml2-config path...

configure: error: xml2-config not found. Please check your libxml2 installation.

解决办法:检查是否安装了libxm包: # rpm -qa |grep  libxml2

     libxml2-2.6.26-2.1.12

     libxml2-python-2.6.26-2.1.12

重新安装libxml2和libxml2-devel包

# yum install libxml2

# yum install libxml2-devel -y

安装完之后查找xml2-config文件是否存在

# find / -name "xml2-config"

/usr/bin/xml2-config

4,编译命令报PHP7 configure: error: Cannot find OpenSSL’s <ev.h>

解决办法:安装opensll:# yum install -y openssl openssl-devel

5,编译命令报If configure fails try --with-vpx-dir=<DIR> configure: error: jpeglib.h not

解决办法:# yum -y install libjpeg-devel

6,编译命令报 configure:error:mcrypt.h not found. Please reinstall libmcrypt

先安装epel,再安装libmcrypt    

解决办法:# yum install -y epel-release

# yum install -y libmcrypt-devel

7,安装PHP:# make

              # make install

  8,修改配置文件:# cp PHP.ini-production /usr/local/PHP/etc/PHP.ini

                     # vim /usr/local/PHP/etc/PHP.ini

[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/sbin/PHP-fpm -t 

9,启动PHP-fpm拷贝启动脚本:# cp /usr/local/src/PHP-5.3.27/sapi/fpm/init.d.PHP-fpm /etc/init.d/PHP-fpm 

修改启动脚本权限:# chmod 755 /etc/init.d/PHP-fpm 

启动PHP:# service PHP-fpm start 

设置开机启动:# chkconfig PHP-fpm on

检测PHP-fpm是否启动:# ps aux |grep PHP-fpm (执行这条命令大概可以看到20多个进程) 

安装Nginx

Nginx官方网站(http://Nginx.org) 

1,下载和解压Nginx:# cd /usr/local/src/

  # wget http://Nginx.org/en/download.html/Nginx-1.20.0

  # tar zxvf Nginx-1.20.0.tar.gz 

2,配置编译选项:# cd Nginx-1.2.9

            # ./configure   --prefix=/usr/local/Nginx  

3,编译和安装Nginx

# make 

# make install

4,编写启动脚本 # 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 

5,保存改脚本之后修改权限 # chmod 755 /etc/init.d/Nginx 

           # chkconfig --add Nginx

加入开机启动 # chkconfig Nginx on 

6,更改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; 

}

include vhost/*.conf; 

}

7,检查配置文件是否有误:# /usr/local/Nginx/sbin/Nginx -t 

8,启动Nginx:# service Nginx start 

    检查Nginx是否启动:# ps aux |grep Nginx

9,测试是否正确解析PHP

创建测试文件:# vim /usr/local/Nginx/html/2.PHP  //内容如下

 编辑内容如下:

<?PHP 

    echo "测试PHP是否解析"; 

 ?>

执行如下命令测试文件:  curl  localhost/2.PHP  


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐