通过LNMP制作个人博客
(1) 创建mysql用户的账号
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -s /sbin/nologin -g mysql -M mysql
[root@localhost ~]# tail -1 /etc/passwd
mysql:x:501:501::/home/mysql:/sbin/nologin
[root@localhost ~]# id mysql
uid=501(mysql) gid=501(mysql) groups=501(mysql)
(2)获取MySQL二进制软件包
百度云盘:http://pan.baidu.com/s/1hrBCzsC
(3) 采用二进制方式安装MySQL
[root@localhost ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv mysql-5.5.32-linux2.6-x86_64 mysql-5.5.32
[root@localhost local]# ln -s mysql-5.5.32 mysql
[root@localhost local]# ls
bin games lib libexec mysql-5.5.32 nginx-1.10.2 share
etc include lib64 mysql nginx sbin src
[root@localhost local]# cd /usr/local/mysql
[root@localhost mysql]# ls
bin data include lib mysql-test scripts sql-bench
COPYING docs INSTALL-BINARY man README share support-files
#提示:
二进制安装包,仅需要解压就可以了,不需要执行cmake/configure,make,make install等过程
4)初始化MySQL配置文件my.cnf
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# /bin/cp support-files/my-small.cnf /etc/my.cnf
(5)初始化MySQL数据库文件
[root@localhost ~]# mkdir -p /usr/local/mysql/data #建立MySQL数据文件目录
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql #授权mysql用户管理MySQL的安装目录
[root@localhost ~]# yum -y install libaio #光盘源安装依赖包,否则下一步的编译会报错
[root@localhost ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
#初始化MySQL数据库文件,会有很多信息提示,如果没有ERROR级别的错误,会有两个OK的字样,表示初始化成功,否则就要解决初始化的问题
配置并启动MySQL数据库
(1)设置MySQL启动脚本,命令如下
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
#拷贝MySQL启动脚本到MySQL的命令路径
[root@localhost mysql]# chmod +x /etc/init.d/mysqld
#使脚本可执行
-
(2)MySQL二进制默认安装路径是/usr/local/mysql,启动脚本里是/usr/local/mysql。如果安装路径不同,那么脚本里路径等都需要替换
(3)启动MySQL数据库,命令如下:
[root@localhost mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
以上是启动数据库的规范方法之一,但还可以用如下方式启动,
/usr/local/mysql/bin/mysqld_safe --user=mysql &
这个命令结尾的“&”符号,作用是在后台执行MySQL服务,命令执行完还需要按下回车才能进入命令行状态。(4)检查MySQL数据库是否启动,命令如下:
[root@localhost mysql]# netstat -antup | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1702/mysqld
-
3.4 LNMP之PHP(FastCGI方式)服务的安装和准备
3.4.1 检查Nginx及MySQL的安装情况
(1)检查确认Nginx及MySQL的安装路径,命令如下:
[root@localhost ~]# ls -ld /usr/local/nginx
lrwxrwxrwx. 1 root root 24 Jul 9 14:31 /usr/local/nginx -> /usr/local/nginx-1.10.2/
[root@localhost ~]# ls -ld /usr/local/mysql
lrwxrwxrwx. 1 mysql mysql 12 Jul 14 07:13 /usr/local/mysql -> mysql-5.5.32
(2)检查端口及启动情况,命令如下:
[root@localhost ~]# netstat -antup | grep -E "80|3306"
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1193/nginx
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1702/mysqld
(3)测试访问Nginx及MySQL是否OK,命令如下:
[root@localhost ~]# wget 127.0.0.1 #测试Nginx
--2017-07-14 09:54:12-- http://127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 624 [text/html]
Saving to: “index.html”
100%[=========================================================================================>] 624 --.-K/s in 0s
2017-07-14 09:54:12 (2.12 MB/s) - “index.html” saved [624/624]
[root@localhost ~]# mysql -uroot -p #测试MySQL
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 6
Server version: 5.5.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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> quit
Bye
如果访问结果和上述一致,就表明Nginx及MySQL的安装一切正常
3.4.2 检查安装PHP所需的lib库
PHP程序在开发及运行时会调用一些诸如zlib,gd等函数库,因此需要确认lib库是否已经安装,执行过程如下:
[root@localhost ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
zlib-devel-1.2.3-29.el6.x86_64
[root@localhost ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxsl
3.4.2 检查安装PHP所需的lib库
PHP程序在开发及运行时会调用一些诸如zlib,gd等函数库,因此需要确认lib库是否已经安装,执行过程如下:
[root@localhost ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
zlib-devel-1.2.3-29.el6.x86_64
[root@localhost ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
执行下面命令安装相关的lib软件包
[root@localhost ~]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
[root@localhost ~]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
安装后的结果如下:
[root@localhost ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
zlib-devel-1.2.3-29.el6.x86_64
libxml2-devel-2.7.6-14.el6.x86_64
libjpeg-turbo-devel-1.2.1-1.el6.x86_64
#这里仅缺少libiconv-devel包
[root@localhost ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
freetype-devel-2.3.11-14.el6_3.1.x86_64
libpng-devel-1.2.49-1.el6_2.x86_64
libcurl-devel-7.19.7-37.el6_4.x86_64
libxslt-devel-1.1.26-2.el6_3.1.x86_64
gd-2.0.35-11.el6.x86_64
3.5.3 编译PHP
正确执行前文配置PHP软件的./configure系列命令后,就可以编译PHP软件了,具体操作过程如下:
[root@localhost php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18
libmysqlclient.so.18 libmysqlclient.so.18.0.0
[root@localhost php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
[root@localhost php-5.3.28]# touch ext/phar/phar.phar
[root@localhost php-5.3.28]# make
#make最后的正确提示
Build complete.
Don't forget to run 'make test'.
3.5.4 安装PHP生成文件到系统
[root@localhost php-5.3.28]# make install
3.5.5 配置PHP引擎配置文件php.ini
(1)设置软链接以方便访问,命令如下:
[root@localhost ~]# ln -s /usr/local/php5.3.28/ /usr/local/php
[root@localhost ~]# ls -l /usr/local/php
lrwxrwxrwx. 1 root root 21 Jul 14 13:06 /usr/local/php -> /usr/local/php5.3.28/
(3)拷贝PHP配置文件到PHP默认目录,并更改文件名称为php.ini,命令如下:
[root@localhost php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini
[root@localhost php-5.3.28]# ls -l /usr/local/php/lib/php.ini
-rw-r--r--. 1 root root 69627 Jul 14 13:25 /usr/local/php/lib/php.ini
3.5.6 配置PHP(FastCGI方式)的配置文件php-fpm.conf
[root@localhost php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini
[root@localhost php-5.3.28]# ls -l /usr/local/php/lib/php.ini
-rw-r--r--. 1 root root 69627 Jul 14 13:25 /usr/local/php/lib/php.ini
[root@localhost php-5.3.28]# cd /usr/local/php/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
关于php-fpm.conf,暂时可用默认的配置,先把服务搭好,以后再进行优化。
3.5.7 启动PHP服务(FastCGI方式)
(1)启动PHP服务php-fpm,命令如下:
[root@localhost etc]# /usr/local/php/sbin/php-fpm
(2)检查PHP服务php-fpm的进程及启动端口的情况,命令如下:
[root@localhost etc]# ps -ef | grep php-fpm
root 126611 1 0 13:36 ? 00:00:00 php-fpm: master process (/usr/local/php5.3.28/etc/php-fpm.conf)
nginx 126612 126611 0 13:36 ? 00:00:00 php-fpm: pool www
nginx 126613 126611 0 13:36 ? 00:00:00 php-fpm: pool www
root 126619 126548 0 13:39 pts/1 00:00:00 grep php-fpm
[root@localhost etc]# lsof -i:9000 #默认9000端口提供服务
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php-fpm 126611 root 7u IPv4 136041 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 126612 nginx 0u IPv4 136041 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 126613 nginx 0u IPv4 136041 0t0 TCP localhost:cslistener (LISTEN)
3.6 配置Nginx支持PHP程序请求访问
3.6.1 修改Nginx配置文件
(1)查看nginx当前的配置,命令如下
最终blog虚拟机的完整配置如下:
[root@localhost conf]# cat extra/blog.conf
server {
listen 80;
server_name blog.yunjisuan.com;
location / {
root /var/www/html/blogcom;
index index.html index.htm;
}
location ~ .*.(php|php5)?$ {
root /var/www/html/blogcom;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
3.6.2 检查并启动Nginx
可通过如下命令检查Nginx配置文件的语法:
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.10.2//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.10.2//conf/nginx.conf test is successful
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload