您的位置:首页 > 运维架构 > Nginx

LNMP架构介绍,MySQL安装,PHP安装,Nginx介绍

2018-07-04 11:41 786 查看

LNMP架构介绍

和lamp不同的是,提供web服务的是nginx

并且php是作为独立服务存在的,这个服务叫做php-fpm

Nginx直接处理静态请求,动态请求会转发给php-fpm

Ngeinx 设计初衷是提供一种快速、高效、多并发的web服务软件。在静态页面处理上,Nginx较Apache更胜一筹。但是在动态页面处理上,Nginx并不能比Apache有优势。但是目前还是有很多爱好者对Nginx比较热衷。随着Nginx技术的逐渐成熟,它在web服务软件领域的地位也会越来越高。

MySQl安装

先到/usr/local/src下 然后去r.aminglinux.com找下载链接,然后wget下载

[root@aminglinux-01 src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz


解压 tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

移动位置 mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql

[root@aminglinux-01 src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
[root@aminglinux-01 src]# ^C
[root@aminglinux-01 src]# cd ../mysql/
[root@aminglinux-01 mysql]# ls
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
[root@aminglinux-01 mysql]#


创建mysql用户 创建/data/目录

useradd -s /sbin/nologin mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql


初始化

[root@aminglinux-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql


报错

FATAL ERROR: please install the following Perl modules before executing scripts/mysql_install_db:
Data::Dumper


安装yum install -y perl-Module-Install.noarch

报错Installing MySQL system tables.../usr/local/mysql/bin/mysqld: error while loading shared

原因:缺少libaio库文件

解决方法:yum install libaio* -y


copy配置文件

cp support-files/my-default.cnf /etc/my.cnf

然后vim /etc/my.cnf

定义如下:

datadir = /data/mysql
# port = .....
# server_id = .....
socket = /tmp/mysql.sock


copy启动脚本

[root@aminglinux-01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld


编辑一下,有两个地方需要改

basedir=/usr/local/mysql
datadir=/data/mysql


启动服务 /etc/init.d/mysqld start

[root@aminglinux-01 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/aminglinux-01.err'.
. SUCCESS!
[root@aminglinux-01 mysql]#

[root@aminglinux-01 mysql]# ps aux |grep mysql
root       4080  0.0  0.1 113256  1612 pts/0    S    22:08   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/aminglinux-01.pid
mysql      4215  2.0 44.9 973048 449532 pts/0   Sl   22:08   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/aminglinux-01.err --pid-file=/data/mysql/aminglinux-01.pid --socket=/tmp/mysql.sock
root       4241  0.0  0.0 112664   976 pts/0    R+   22:09   0:00 grep --color=auto mysql
[root@aminglinux-01 mysql]#


把mysql加入到服务行列 chkconfig --add mysqld

设置开机启动 chkconfig mysqld on

这时候就可以执行 service mysqld stop 或者 start 的命令了。

PHP安装

在lnmp里面php的安装和lamp安装是有差别的。在这里php需要开启php-fpm服务,是一个独立的存在。

安装

cd /usr/local/src/

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/

初始化

yum install -y epel-release
yum -y install gcc libxml2-devel openssl openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-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-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-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 --with-pear --with-curl  --with-openssl


报错checking for cc... no
checking for gcc... no

yum install gcc -y


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

yum install libxml2-devel -y


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

yum install -y openssl openssl-devel


报错configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/

yum install libcurl-devel -y


报错:configure: error: jpeglib.h not found.

yum install -y libjpeg-devel


报错:configure: error: png.h not found.

yum install -y libpng-devel


报错: configure: error: freetype-config not found.

yum install -y freetype-devel


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

yum install -y epel-release  先安装扩展源
yum install -y libmcrypt-devel


make && make install

报错:libtool: link: `ext/opcache/ZendAccelerator.lo' is not a valid libtool object

解决办法
# make distclean
重新初始化,安装


安装成功后,可以看一下这个目录,是启动php-fpm服务用的

[root@aminglinux-01 php-fpm]# cd /usr/local/php-fpm/sbin/
[root@aminglinux-01 sbin]# ls
php-fpm


/usr/local/php-fpm/sbin/php-fpm -t 是测试它自己配置文件的语法有没有问题的

[root@aminglinux-01 sbin]# /usr/local/php-fpm/sbin/php-fpm -t
[19-Oct-2017 09:58:56] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[19-Oct-2017 09:58:56] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[19-Oct-2017 09:58:56] ERROR: FPM initialization failed
[root@aminglinux-01 sbin]#


发现配置文件不存在,配置它,让服务起来。

cp /usr/local/src/php-5.6.30/php.ini-production /usr/local/php-fpm/etc/php.ini

创建/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


cp启动脚本

[root@aminglinux-01 php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm


然后加到服务列表里去,首先要改权限,添加列表,开机启动

[root@aminglinux-01 php-5.6.30]# chmod 755 /etc/init.d/php-fpm
[root@aminglinux-01 php-5.6.30]# chkconfig --add php-fpm
[root@aminglinux-01 php-5.6.30]# chkconfig php-fpm on


启动和查看

[root@aminglinux-01 php-5.6.30]# service php-fpm start
Starting php-fpm  done
[root@aminglinux-01 php-5.6.30]# ps aux |grep php-fpm
root      73918  0.0  0.4 123548  4960 ?        Ss   10:31   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   73919  0.0  0.4 123548  4712 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73920  0.0  0.4 123548  4712 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73921  0.0  0.4 123548  4712 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73922  0.0  0.4 123548  4712 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73923  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73924  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73925  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73926  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73927  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73928  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73929  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73930  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73931  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73932  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73933  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73934  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73935  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73936  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73937  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73938  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
root      73940  0.0  0.0 112668   976 pts/0    R+   10:31   0:00 grep --color=auto php-fpm
[root@aminglinux-01 php-5.6.30]#


Nginx安装

到src下wget http://nginx.org/download/nginx-1.8.0.tar.gz

解压:tar zxvf nginx-1.8.0.tar.gz

cd nginx-1.8.0

初始化 ./configure --prefix=/usr/local/nginx

make && make install

安装Nginx时报错

./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel解决问题

yum -y install pcre-devel

错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

解决办法:

yum -y install openssl openssl-devel

总结:

yum -y install pcre-devel openssl openssl-devel

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

make

make install

一切搞定


下面要给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


然后改权限,添加服务,设置开机自启

[root@aminglinux-01 nginx-1.8.0]# chmod 755 /etc/init.d/nginx
[root@aminglinux-01 nginx-1.8.0]# chkconfig --add nginx
[root@aminglinux-01 nginx-1.8.0]# chkconfig nginx on


cd /usr/local/nginx/conf/ 到这个目录里,配置一下nginx.conf,这里面已经有一个了,我们不用他,自己配置

[root@aminglinux-01 nginx-1.8.0]# cd /usr/local/nginx/conf/
[root@aminglinux-01 conf]# mv nginx.conf nginx.conf.bak


编辑配置文件如下 vim 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

启动 : /etc/init.d/nginx start

[root@aminglinux-01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  确定  ]
[root@aminglinux-01 conf]# ps aux |grep nginx
root      77488  0.0  0.0  24828   792 ?        Ss   22:08   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    77489  0.0  0.3  27272  3376 ?        S    22:08   0:00 nginx: worker process
nobody    77490  0.0  0.3  27272  3376 ?        S    22:08   0:00 nginx: worker process
root      77492  0.0  0.0 112668   976 pts/0    S+   22:09   0:00 grep --color=auto nginx
[root@aminglinux-01 conf]#

[root@aminglinux-01 conf]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@aminglinux-01 conf]#


测试php解析

vi /usr/local/nginx/html/1.php //加入如下内容

<?php
echo "test php scripts.";
?>


curl localhost/1.php

Nginx介绍

Nginx官网 nginx.org,最新版1.13,最新稳定版1.12

Nginx应用场景:web服务、反向代理、负载均衡

Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并

Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928

扩展

Nginx为什么比Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html
mod_php 和 mod_fastcgi以及php-fpm的比较 http://dwz.cn/1lwMSd
概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM http://www.nowamagic.net/librarys/veda/detail/1319/ https://www.awaimai.com/371.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP OpenSSL LNMP MySQL php-fpm