一、系统环境
下面用一个以最小化方式(Minimal)安装的CentOS 6.9操作系统为例,演示LNMP环境完整搭建过程,并且最后部署了一个WordPress博客,最后完成的效果如下:
二、安装Nginx
[root@localhost ~]# yum -y install pcre pcre-devel openssl-devel openssh
[root@localhost ~]# rpm -qa pcre pcre-devel openssl openssl-devel
2、下载安装Nginx,这里使用的是nginx-1.13.4
[root@localhost ~]# yum -y install wget
[root@localhost ~]# wget -c -P /src https://mirrors.yangxingzhen.com/nginx/nginx-1.13.4.tar.gz
[root@localhost ~]# ls /src
[root@localhost ~]# cd /src
[root@localhost src]# tar zxf nginx-1.13.4.tar.gz
[root@localhost ~]# yum -y install gcc gcc-c++
[root@localhost ~]# useradd -s /sbin/nologin www
[root@localhost ~]# cd /src/nginx-1.13.4
[root@localhost nginx-1.13.4]# ./configure --user=www --group=www --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module
[root@localhost nginx-1.13.4]# echo $?
[root@localhost nginx-1.13.4]# make && make install
[root@localhost nginx-1.13.4]# echo $?
5、测试Nginx,启动Nginx
[root@localhost nginx-1.13.4]# /usr/local/nginx/sbin/nginx -t
[root@localhost nginx-1.13.4]# /usr/local/nginx/sbin/nginx
#因为要搭建一个博客服务,所以这里配置的域名为blog.admin.org,操作过程如下:
[root@localhost conf]# cd /usr/local/nginx/conf/
[root@localhost conf]# grep –vE "^$|#" nginx.conf.default > nginx.conf (过滤空行跟#开头的注释行)
[root@localhost conf]# cat nginx.conf
7、修改Nginx配置文件
[root@localhost conf]# vim nginx.conf
8、创建域名对应的站点目录及文件
[root@localhost conf]# cd ../html
[root@localhost html]# mkdir blog
[root@localhost html]# echo "This page is: blog.admin.org" > blog/index.html
[root@localhost html]# cat blog/index.html
[root@localhost html]# ../sbin/nginx -t
[root@localhost html]# ../sbin/nginx -s reload (平滑重启)
10、本机上进行测试
[root@localhost html]# echo "127.0.0.1 blog.admin.org" >> /etc/hosts
[root@localhost html]# curl blog.admin.org
[root@localhost html]# wget blog.admin.org
11、在Windows 7测试(需要添加hosts文件)
C:/Windows/System32/drivers/etc/hosts,添加116.196.117.83 blog.admin.org
三、安装MySQL
[root@localhost ~]# cd /src
[root@localhost src]# wget -c https://mirrors.yangxingzhen.com/mysql/mysql-5.6.36.tar.gz
[root@localhost src]# ls -l
[root@localhost src]# tar zxf mysql-5.6.36.tar.gz
[root@localhost src]# useradd -s /sbin/nologin mysql
[root@localhost src]# mkdir -p /usr/local/mysql
[root@localhost src]# chown -R mysql.mysql /usr/local/mysql
[root@localhost src]# yum -y install cmake ncurses-devel perl
[root@localhost src]# cd mysql-5.6.36
[root@localhost mysql-5.6.36]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql /
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock /
-DWITH_INNOBASE_STORAGE_ENGINE=1 /
-DWITH_PARTITION_STORAGE_ENGINE=1 /
-DWITH_MYISAM_STORAGE_ENGINE=1 /
-DWITH_EXDDEFAULT_CHARSET=utf8 /
-DDEFAULT_COLLATION=utf8_general_ci /
[root@localhost mysql-5.6.36]# make &&make install
5、修改配置文件
编辑vim /etc/my.cnf,内容如下
[root@localhost mysql-5.6.36]# vim /etc/my.cnf
[mysqld]
datadir = /data/mysql
socket = /tmp/mysql.sock
user = mysql
log-error = /var/log/mysqld.log
character_set_server = utf8
[client]
default-character-set = utf8
[mysql]
default-character-set = utf8
6、初始化数据库
[root@localhost ~]# mkdir -p /data/mysql
[root@localhost ~]# chown -R mysql.mysql /data/mysql
[root@localhost ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql
[root@localhost ~]# ln -s /usr/local/mysql/bin/* /usr/bin
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chmod o+x /etc/init.d/mysqld
[root@localhost ~]# /etc/init.d/mysqld start
#检查端口、进程
[root@localhost ~]# netstat -lntup | grep mysqld
[root@localhost ~]# ps -ef | grep mysqld
8、设置MySQL开机启动
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# chkconfig --list mysqld
[root@localhost ~]# mysql
mysql> show databases;
四、安装PHP
# 安装Libiconv
1、安装PHP依赖函数库
[root@localhost ~]# yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
2、安装libmcrypt库、mhash加密扩展库、mcrypt加密扩展库
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
[root@localhost ~]# yum -y install libmcrypt-devel mhash mcrypt
[root@localhost ~]# wget -c https://mirrors.yangxingzhen.com/libiconv/libiconv-1.14.tar.gz
[root@localhost ~]# tar zxf libiconv-1.14.tar.gz
[root@localhost ~]# cd libiconv-1.14
[root@localhost libiconv-1.14]# ./configure --prefix=/usr/local/libiconv
[root@localhost libiconv-1.14]# make &&make install
# 安装PHP
[root@localhost libiconv-1.14]# cd /src
[root@localhost src]# wget -c http://mirrors.sohu.com/php/php-5.6.29.tar.gz
[root@localhost src]# tar zxf php-5.6.29.tar.gz
[root@localhost src]# ls -l
[root@localhost src]# cd /src/php-5.6.29
[root@localhost php-5.6.29]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64
[root@localhost php-5.6.29]# ./configure --prefix=/usr/local/php /
--with-config-file-path=/usr/local/php/etc /
--with-mysql=/usr/local/mysql /
--with-mysqli=/usr/local/mysql/bin/mysql_config /
--with-pdo-mysql=/usr/local/mysql /
--with-iconv-dir=/usr/local/libiconv /
--enable-inline-optimization /
[root@localhost php-5.6.29]# make &&make install
[root@localhost php-5.6.29]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-5.6.29]# /usr/local/php/sbin/php-fpm
#LNMP环境测试
[root@localhost php-5.6.29]# cd /usr/local/nginx/html/blog/
[root@localhost blog]# echo "<? phpinfo(); ?>" > test.php
[root@localhost blog]# /usr/local/nginx/sbin/nginx -t
[root@localhost blog]# /usr/local/nginx/sbin/nginx -s reload
3、Windows7主机上在浏览器中输入地址http://blog.admin.org/test.php进行访问
五、安装WordPress
[root@localhost blog]# mysql
#创建数据库wordpress、创建wordpress用户并授权
mysql> create database wordpress;
mysql> grant all on wordpress.* to wordpress@"localhost" identified by '123456';
mysql> flush privileges;
[root@localhost blog]# cd /src
[root@localhost src]# wget -c https://mirrors.yangxingzhen.com/wordpress/wordpress-4.9.1-zh_CN.tar.gz
3、解压软件包
[root@localhost src]# tar zxf wordpress-4.9.1-zh_CN.tar.gz
4、拷贝WordPress程序到blog目录下,对blog下所有文件授予www用户和组的权限
[root@localhost src]# cd wordpress
[root@localhost wordpress]# cp -a * /usr/local/nginx/html/blog
[root@localhost wordpress]# chown -R www.www /usr/local/nginx/html/blog
5、访问安装WordPress
#在windows7主机浏览器输入http://blog.admin.org
接下来的安装都是非常人性化的,点击"现在就开始",出现下面的页面:
显示上面的页面,就说明我们的WordPress安装成功了!接下来就可以好好管理自己的个人WordPress博客站点了!