LNMP环境搭建与配置

和LAMP不同的是,LNMP中的N指的是Nginx(类似于Apache的一种web服务软件),并且php是作为一个独立服务存在的,这个服务叫做php-fpm,Nginx直接处理静态请求,动态请求会转发给php-fpm。

目前LNMP环境的应用非常多。在静态页面的处理上,Nginx比Apache更强;但在动态页面的处理上,Nginx并不占优势。

12.1 安装MySQL
LNMP中MySQL的安装步骤和LAMP一样。

下载软件包:
# cd /usr/local/src/

# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz #下载mysql二进制包

初始化:
# tar zxf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz #解压二进制包

# [ -d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_old

# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql

# useradd -s /sbin/nologin mysql #创建用户mysql

# cd /usr/local/mysql

# mkdir -p data/mysql #创建datadir,数据库文件会放到这里

# chown -R mysql:mysql data/mysql #更改权限,否则后面会出问题

# ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql #这里datadir尽量使用绝对路径,不然后面可能报错
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper #有报错,安装所缺包

# yum list |grep -i dumper
perl-Data-Dumper.x86_64 2.145-3.el7 @base
perl-XML-Dumper.noarch 0.81-17.el7 base

# yum install -y perl-Data-Dumper.x86_64

# ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory #再次报错,安装所缺包

# yum install -y libaio-devel

# ./scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/mysql #有两个OK,就说明初始化成功

# echo $?
#检验上条命令是否执行成功,0表示执行成功

配置MySQL:
# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y

# vim /etc/my.cnf #修改配置文件如下

[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 #这里做3处修改,basedir 是MySQL包所在的路径,datadir 是定义的存放数据的地方,port 定义MySQL服务监听的端口,如果不定义默认就是3306
datadir = /usr/local/mysql/data/mysql
port = 3306
# server_id = .....
# socket = .....

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

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

# chmod 755 /etc/init.d/mysqld

# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data/mysql #修改成这样

# chkconfig --add mysqld #将mysqld加入系统服务项

# chkconfig mysqld on #设置开机启动

启动MySQL:
# service mysqld start #启动mysqld服务
Starting MySQL.Logging to '/usr/local/mysql/data/mysql/localhost.localdomain.err'.
. SUCCESS! #mysqld服务启动成功

# netstat -lntp |grep 3306 #检验mysqld服务是否启动成功,查看是否在监听3306端口
tcp6 0 0 :::3306 :::* LISTEN 3655/mysqld

12.2 安装PHP
下载源码包:
# cd /usr/local/src/

# wget http://cn2.php.net/distributions/php-5.6.36.tar.gz

解压源码包,创建账号:
# tar zxf php-5.6.36.tar.gz

# useradd -s /sbin/nologin php-fpm

配置编译选项:
# ./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 #多了--enable-fpm,如果不加该参数,则不会有php-fpm执行文件生成,更不能启动php-fpm服务

错误1:
checking for cc... no
checking for gcc... no
configure: error: in `/usr/local/src/php-5.6.36':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

# yum install -y gcc #这里提示安装gcc,是因为我用了新的虚拟机

# ./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 #继续执行这一步

错误2:
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.

# yum list |grep libxml2
libxml2.x86_64 2.9.1-6.el7_2.3 @anaconda
libxml2.i686 2.9.1-6.el7_2.3 base
libxml2-devel.i686 2.9.1-6.el7_2.3 base
libxml2-devel.x86_64 2.9.1-6.el7_2.3 base
libxml2-python.x86_64 2.9.1-6.el7_2.3 base
libxml2-static.i686 2.9.1-6.el7_2.3 base
libxml2-static.x86_64 2.9.1-6.el7_2.3 base

# yum install -y libxml2-devel.x86_64 #安装libxml-devel

# ./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 #继续执行这一步

错误3:
configure: error: Cannot find OpenSSL's <evp.h>

# yum install -y openssl openssl-devel #安装openssl和openssl-devel

# ./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 #继续执行这一步

错误4:
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/

# yum install -y libcurl-devel #安装libcurl-devel

# ./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:
configure: error: jpeglib.h not found.

# yum -y install libjpeg-devel #安装libjpeg-devel

# ./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 #继续执行这一步

错误6:
configure: error: png.h not found.

# yum install -y libpng libpng-devel #安装libpng-devel

# ./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 #继续执行这一步

错误7:
configure: error: freetype-config not found.

# yum install -y freetype freetype-devel #安装freetype-devel

# ./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 #继续执行这一步

错误8:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.

# yum install -y epel-release

# yum install -y libmcrypt-devel #安装libmcrypt-devel(安装之前要安装epel-release这个扩展源)

# ./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 #继续执行这一步

终于不再提示错误,有这样的信息:
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

这就说明PHP配置编译参数完成。

编译php:
# make
Build complete.
Don't forget to run 'make test'.

# echo $?

编译完成(在这一步,也有可能会遇到问题)。

安装php:
# make install
Wrote PEAR system config file at: /usr/local/php-fpm/etc/pear.conf
You may want to add: /usr/local/php-fpm/lib/php to your php.ini include_path
/usr/local/src/php-5.6.36/build/shtool install -c ext/phar/phar.phar /usr/local/php-fpm/bin
ln -s -f phar.phar /usr/local/php-fpm/bin/phar
Installing PDO headers: /usr/local/php-fpm/include/php/ext/pdo/

# echo $?

安装完成。

修改配置文件:
# 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
[01-Jul-2018 21:08:57] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful #显示 test is successful ,说明配置没有问题

启动php-fpm:
# 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
useradd:用户“php-fpm”已存在 #如果之前进行过这一步,那这里就可以省略

# service php-fpm start
Starting php-fpm done

# ps aux |grep php-fpm #检测php-fpm是否启动

root 30222 0.0 0.1 123452 4812 ? Ss 21:14 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 30223 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30224 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30225 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30226 0.0 0.1 123452 4580 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30227 0.0 0.1 123452 4584 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30228 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30229 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30230 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30231 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30232 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30233 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30234 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30235 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30236 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30237 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30238 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30239 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30240 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30241 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
php-fpm 30242 0.0 0.1 123452 4588 ? S 21:14 0:00 php-fpm: pool www
root 30248 0.0 0.0 112720 980 pts/0 S+ 21:16 0:00 grep --color=auto php-fpm #说明php-fpm成功启动

# chkconfig php-fpm on #设置php-fpm开机启动

12.3 安装Nginx
下载和解压Nginx:
# cd /usr/local/src/

# wget http://nginx.org/download/nginx-1.12.1.tar.gz

# tar zxf nginx-1.12.1.tar.gz

配置编译选项:
# cd nginx-1.12.1

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

Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

# echo $?


编译和安装Nginx:
# make

# echo $?

# make install

# echo $?

编写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开机启动

更改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 #检验配置文件是否有问题
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful #显示上面两行说明配置正确

启动Nginx:
# service nginx start #启动Nginx服务
Starting nginx (via systemctl): [ 确定 ] #如果不能启动,可以查看/usr/local/nginx/logs/error.log文件

# ps aux |grep nginx #检验Nginx服务是否启动
root 32821 0.0 0.0 20540 624 ? Ss 21:47 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 32822 0.0 0.0 22984 3204 ? S 21:47 0:00 nginx: worker process
nobody 32823 0.0 0.0 22984 3204 ? S 21:47 0:00 nginx: worker process
root 32825 0.0 0.0 112720 984 pts/0 S+ 21:48 0:00 grep --color=auto nginx
可以看到,Nginx服务成功启动。

测试是否正确解析PHP:
# vim /usr/local/nginx/html/1.php
<?php
echo "php解析正常";
?>

# curl localhost/1.php
php解析正常[root@localhost nginx-1.12.1]#
说明PHP解析正常。
————————————————
原文链接:https://blog.csdn.net/miss1181248983/article/details/80890649

原文地址:https://www.cnblogs.com/zhaoshaopeng/p/12869361.html