1、介绍
LNMP(Linux+Nginx+Mysql+PHP) WEB架构中,Nginx为一款高性能Web服务器,本身是不能处理PHP的,当接收到客户端浏览器发送的HTTP Request请求时,Nginx服务器响应并处理web请求。对于静态资源CSS、图片、视频、TXT等文件请求,Nginx服务器可以直接处理并回应。 但是PHP动态页面请求Nginx不能直接处理,Nginx服务器会将PHP网页脚本通过接口传输协议(网关协议)PHP-FCGI(Fast-CGI)传输给PHP-FPM(进程管理程序),PHP-FPM不做处理,然后PHP-FPM调用PHP解析器进程,PHP解析器解析PHP脚本信息。PHP解析器进程可以启动多个,可以实现多进程并发执行。PHP解析器将解析后的脚本返回到PHP-FPM,PHP-FPM再通过Fast-CGI的形式将脚本信息传送给 Nginx,Nginx服务器再通过Http Response的形式传送给浏览器,浏览器再进行解析与渲染然后进行呈现。
本章介绍如何单机实现LNMP部署wordpress网站。
2、安装Mysql
2.1 安装
[root@localhost ~]# cd /usr/src
[root@localhost src]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.5/mysql-5.5.60.tar.gz
[root@localhost src]# tar xf mysql-5.5.60.tar.gz cd mysql-5.5.60/
[root@localhost src]# yum install gcc ncurses-devel libaio bison gcc-c++ git cmake ncurses-devel ncurses -y
[root@localhost src]# cd mysql-5.5.60/
2.2 预编译
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql55/ \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_BIG_TABLES=1 \
-DWITH_DEBUG=0
2.3 编译、安装
[root@localhost mysql-5.5.60]# make && make install
2.4 配置
[root@localhost mysql-5.5.60]# cp support-files/my-large.cnf /usr/local/mysql55/my.cnf
[root@localhost mysql-5.5.60]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql-5.5.60]# chmod +x /etc/init.d/mysqld
[root@localhost mysql-5.5.60]# mkdir -p /data/mysql
[root@localhost mysql-5.5.60]# useradd -s /sbin/nologin mysql
[root@localhost mysql-5.5.60]# chown -R mysql. /data/mysql
[root@localhost mysql-5.5.60]# /usr/local/mysql55/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql55
[root@localhost mysql-5.5.60]# /etc/init.d/mysqld start
2.5 添加系统服务
# 加入service服务:
[root@localhost mysql-5.5.60]# chkconfig --add mysqld
[root@localhost mysql-5.5.60]# chkconfig --level 35 mysqld on
3、安装Nginx
# 下载源码:
[root@localhost src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
# 解压:
[root@localhost src]# tar xf nginx-1.16.0.tar.gz
[root@localhost src]# cd nginx-1.16.0/
# 预编译:
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module
# 编译/安装:
[root@localhost nginx-1.16.1]# make && make install
# 修改nginx进程用户为nginx:
[root@localhost nginx-1.16.1]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
# 启动nginx:
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx
4、安装PHP
4.1 安装
# 安装依赖:
[root@localhost src]# yum -y install gd curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel
#下载包:
[root@localhost src]# wget http://cn2.php.net/distributions/php-5.6.8.tar.gz
# 解压包:
[root@localhost src]# tar xf php-5.6.8.tar.gz
[root@localhost src]# cd php-5.6.8
4.2 预编译
./configure --prefix=/usr/local/php \
--enable-fpm \
--enable-debug \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--enable-mbstring \
--with-curl \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-config-file-path=/usr/local/php/etc \
--with-zlib-di
4.3 编译、安装
[root@localhost php-5.6.8]# make && make install
4.4 配置
[root@localhost php-5.6.8]# cp php.ini-development /usr/local/php/etc/php.ini
[root@localhost php-5.6.8]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-5.6.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.6.8]# chmod +x /etc/init.d/php-fpm
4.5 修改php-fpm的用户为nginx
[root@localhost php-5.6.8]# vim /usr/local/php/etc/php-fpm.conf
user = nginx
group = nginx
#启动php-fpm
/etc/init.d/php-fpm start
5、上传wordpress网站代码
# 解压包到nginx发布目录:
[root@localhost src]# tar xf wordpress-4.9.4-zh_CN.tar.gz -C /usr/local/nginx/html/
#修改目录权限
[root@localhost src]# chown nginx. -R /usr/local/nginx/html/wordpress/
6、创建wordpress虚拟主机
修改nginx配置并重启
[root@localhost src]# vim /usr/local/nginx/conf/nginx.conf
# 指定应用的虚拟主机目录(主配置文件http指令块下添加):
include vhost/*.conf;
[root@localhost src]# /usr/local/nginx/sbin/nginx -s reload
创建并修改虚拟主机配置文件
# 创建虚拟主机目录:
[root@localhost src]# mkdir -p /usr/local/nginx/conf/vhost
# 创建虚拟主机配置文件(复制nginx.conf中server代码块并修改):
[root@localhost src]# vim /usr/local/nginx/conf/vhost/blog.wordpress.com.conf
server {
listen 80;
server_name blog.wordpress.com;
#charset koi8-r;
access_log logs/wordpress.access.log main;
location / {
root html/wordpress;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
7、创建数据库
# 启动数据库服务:
[root@localhost src]# /etc/init.d/mysqld start
# 进入数据库创建数据库,并授权:
mysql> create database wordpress charset utf8;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on wordpress.* to "wordpress"@localhost identified by "123456";
Query OK, 0 rows affected (0.10 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
8、访问wordpress
8.1 在物理机做好hosts解析,则可通过域名进行访问。
#10.0.0.136为虚拟机的IP
10.0.0.136 blog.wordpress.com
8.2 浏览器访问blog.wordpress.com
至此,大功告成!