LNMP架构
简介:
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统。
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
特点
Nginx是一个小巧而高效的Linux下的Web服务器软件,是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler 站点开发的,已经在一些俄罗斯的大型网站上运行多年,相当的稳定。
Nginx性能稳定、功能丰富、运维简单、处理静态文件速度快且消耗系统资源极少。
优点
作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。
作为负载均衡服务器:Nginx 既可以在内部直接支持Rails和PHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。
作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。
Nginx 安装非常的简单,配置文件非常简洁(还能够支持perl语法)。Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。
系统环境: RHEL6 x86-64 selinux and iptables disabled
一. mysql安装编译
软件包依赖性:
yum install -y gcc gcc-c++ make ncurses-devel bison openssl-devel zlib-devel cmake
tar zxvf mysql-5.5.12.tar.gz
cd mysql-5.5.12
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \#安装目录
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \#数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \#Unix socket 文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 \#安装 myisam 存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \#安装 innodb 存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \#安装 archive 存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \#安装 blackhole 存储引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 \#安装数据库分区
-DENABLED_LOCAL_INFILE=1 \#允许从本地导入数据
-DWITH_READLINE=1 \#快捷键功能
-DWITH_SSL=yes \#支持 SSL
-DDEFAULT_CHARSET=utf8 \#使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \#校验字符
-DEXTRA_CHARSETS=all \#安装所有扩展字符集
-DMYSQL_TCP_PORT=3306 \#MySQL 监听端口
make && make install
[[email protected] mysql]# cd support-files/
[[email protected] support-files]# ls
magic my-default.cnf mysqld_multi.server mysql-log-rotate mysql.server
[[email protected] support-files]# cp my-default.cnf /etc/my.cnf
[[email protected] mysql]# vim /etc/my.cnf
更改以下内容:
# These are commonly set, remove the # and set as required.
basedir = /usr/local/lnmp/mysql
datadir = /usr/local/lnmp/mysql/data
port = 3306
# server_id = .....
socket = /usr/local/lnmp/mysql/data/mysql.sock
[[email protected] support-files]# cp mysql.server /etc/init.d/mysqld
[[email protected] support-files]# groupadd -g 27 mysql
[[email protected] support-files]# useradd -u 27 -g 27 mysql
[[email protected] support-files]# id mysql
uid=27(mysql) gid=27(mysql) groups=27(mysql)
[[email protected] support-files]# cd ..
[[email protected] mysql]# chown -R mysql.mysql .
[[email protected] bin]# vim ~/.bash_profile
更改内容:
PATH=$PATH:/usr/local/mysql/bin
[[email protected] bin]# source ~/.bash_profile
[[email protected] mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/lnmp/mysql --datadir=/usr/local/lnmp/mysql/data
[[email protected] mysql]# chown -R mysql data
[[email protected] mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/lnmp/mysql/data/server1.err'.
. SUCCESS!
[[email protected] mysql]# mysql_secure_installation #更改密码为REDHAT.org123
[[email protected] mysql]# /etc/init.d/mysqld start
[[email protected] mysql]# /etc/init.d/mysqld status
SUCCESS! MySQL running (2452)
[[email protected] mysql]# bin/mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.17 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.08 sec)
二. PHP安装编译
软件包依赖性:
yum install net-snmp-devel curl-devel libxml2-devel libpng-devel libjpeg-devel freetype-devel gmp-devel openldap-devel -y
yum install -y gcc libmcrypt libmcrypt-devel(需下载安装包或源码编译)
源码编译:
tar jxf php-5.6.35.tar.bz2
cd php-5.6.35
./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local
/lnmp/php/etc --with-mysql=/usr/local/lnmp/mysql/ --enable-mysqlnd --with-
mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl
--with-snmp --with-gd - -with-zlib --with-curl --with-libxml-dir --with-png-dir
--with-jpeg-dir --with-freetype-dir --with-pear --with-gettext --enable-inline-
optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring
--enable-fpm --with-mcrypt --with-mhash --with-gmp
make
make install
[[email protected] php-5.6.35]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
[[email protected] php-5.6.35]# cd sapi/
[[email protected] sapi]# cd fpm/
[[email protected] fpm]# ls
config.m4 init.d.php-fpm.in php-fpm.8 php-fpm.service tests
CREDITS LICENSE php-fpm.8.in php-fpm.service.in www.conf.in
fpm Makefile.frag php-fpm.conf status.html
init.d.php-fpm php-fpm php-fpm.conf.in status.html.in
[[email protected] fpm]# cp init.d.php-fpm /etc/init.d/
[[email protected] init.d]# mv init.d.php-fpm php-fpm
[[email protected] init.d]# chmod +x php-fpm
[[email protected] init.d]# cd /usr/local/lnmp/php/etc/
[[email protected] etc]# cp php-fpm.conf.default php-fpm.conf
[[email protected] etc]# vim php-fpm.conf
pid = run/php-fpm.pid #去点之前注释
[[email protected] etc]# useradd nginx
[[email protected] etc]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[[email protected] etc]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 921/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 998/master
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 8985/php-fpm
tcp 0 0 172.25.100.1:22 172.25.100.250:52824 ESTABLISHED 1382/sshd
tcp 0 0 172.25.100.1:22 172.25.100.250:53778 ESTABLISHED 2513/sshd
tcp 0 0 :::22 :::* LISTEN 921/sshd
tcp 0 0 ::1:25 :::* LISTEN 998/master
tcp 0 0 :::3306 :::* LISTEN 2452/mysql
三. NGINX安装编译
软件包依赖性:
[[email protected] etc]# yum install -y pcre-devel
[[email protected] lnmp]# tar zxf nginx-1.14.0.tar.gz
[[email protected] lnmp]# cd nginx-1.14.0
[[email protected] nginx-1.14.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[[email protected] nginx-1.14.0]# vim auto/cc/gcc
[[email protected] nginx-1.14.0]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_stub_status_module --with-http_ssl_module --with-threads --with-file-aio
[[email protected] nginx-1.14.0]# make
[[email protected] nginx-1.14.0]# make install
[[email protected] nginx-1.14.0]# cd conf
[[email protected] conf]# ls
fastcgi.conf koi-utf mime.types scgi_params win-utf
fastcgi_params koi-win nginx.conf uwsgi_params
[[email protected] conf]# vim nginx.conf
更改内容:
user nginx nginx;
worker_processes auto;
events {
worker_connections 65535;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
[[email protected] nginx]# cd html
[[email protected] html]# ls
50x.html index.html
[[email protected] html]# vim index.php
<?php
phpinfo()
?>
[[email protected] nginx]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[[email protected] nginx]# nginx
[[email protected] nginx]# nginx -s reload
更改Nginx默认发布目录:
[[email protected] etc]# vim /usr/local/lnmp/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
[[email protected] etc]# nginx -s reload
1010 ; Default socket name for local MySQL connects. If empty, uses the built-in
1011 ; MySQL defaults.
1012 ; http://php.net/pdo_mysql.default-socket
1013 pdo_mysql.default_socket=/usr/local/lnmp/mysql/data/mysql.sock
1159 ; Default socket name for local MySQL connects. If empty, uses the built-in
1160 ; MySQL defaults.
1161 ; http://php.net/mysql.default-socket
1162 mysql.default_socket = /usr/local/lnmp/mysql/data/mysql.sock
1218 ; Default socket name for local MySQL connects. If empty, uses the built-in
1219 ; MySQL defaults.
1220 ; http://php.net/mysqli.default-socket
1221 mysqli.default_socket = /usr/local/lnmp/mysql/data/mysql.sock
未完。。。