CentOS6.7安装部署LNMP(nginx1.8.0+php5.6.10+mysql5.6.12)
IP-10.0.0.8
1.安装nginx
mkdir -p /server/tools cd /server/tools yum install -y pcre pcre-devel openssl openssl-devel gcc gcc+ wget http://nginx.org/download/nginx-1.8.0.tar.gz useradd www -M -s /sbin/nologin tar xf nginx-1.8.0.tar.gz cd nginx-1.8.0/ sed -i "179s/#//" auto/cc/gcc mkdir /application ./configure --prefix=/application/nginx-1.8.0 --user=www \ --group=www --with-http_stub_status_module --with-http_ssl_module make && make install ln -s /application/nginx-1.8.0/ /application/nginx # Centos7采用yum方式安装nginx rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm yum -y install nginx systemctl start nginx.service
2.安装php
yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel\ libpng-devel freetype-devel libcurl-devel gd-devel libxslt-devel \ mhash mcrypt libmcrypt libmcrypt-devel # libiconv-devel没有这包 cd /server/tools/ wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz tar xf libiconv-1.14.tar.gz cd libiconv-1.14/ ./configure --prefix=/usr/local/libiconv make && make install # PHP的编译参数 cd /server/tools/ wget http://cn2.php.net/distributions/php-5.6.10.tar.gz tar -zxvf php-5.6.10.tar.gz cd php-5.6.10 ./configure --prefix=/opt/php --with-mysql \ --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \ --with-iconv-dir=/usr/local/libiconv --with-zlib \ --with-libxml-dir --enable-xml --with-curl --enable-fpm \ --enable-mbstring --with-gd --with-openssl --with-mhash \ --enable-sockets --with-xmlrpc --enable-zip --enable-soap \ --with-libdir=/usr/lib64 --with-jpeg-dir=/usr/lib64 \ --with-freetype-dir=/usr/lib64 --with-png-dir=/usr/lib64 \ --with-xsl --with-fpm-user=www --with-fpm-group=www make make install clean
3.解决问题
# 安装MySQL参考上一篇博文 # 解决缺少共享库的问题,两种方法(先用find查到这个库的位置): vi /etc/ld.so.conf /opt/mysql-5.6.21/libmysql/ ldconfig # 生效 ln -s /opt/mysql-5.6.21/libmysql/libmysqlclient.so.18 /usr/lib64/ # 如果make的时候报:ext/phar/phar.phar没有这个文件 cd php-5.6.10 touch ext/phar/phar.phar # 本机没有安装mysql时,可以使用下面的参数 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd # 配置并启动php-fpm cp php.ini-production /opt/php/lib/php.ini cd /opt/php/etc/ cp php-fpm.conf.default php-fpm.conf cd /server/tools/php-5.6.10/sapi/fpm/ cp init.d.php-fpm /etc/init.d/php-fpm chmod +x /etc/init.d/php-fpm service php-fpm start # 或者/opt/php/sbin/php-fpm cat blog.conf server { listen 80; server_name blog.etiantian.com; location / { root html/blog; index index.php index.html; } location ~.*\.(php|php5)?$ { root html/blog; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } } /application/nginx/sbin/nginx -s reload mkdir /application/nginx/html/blog/ echo "<?php phpinfo(); ?>" > /application/nginx/html/blog/test.php # 连接数据库测试 cat /application/nginx/html/blog/conn_mysql.php <?php $con = mysql_connect("localhost","root","root123") or mysql_error(); if ($con){ echo "connect mysql successfully"; } else{ echo mysql_error(); } ?>
4.搭建WordPress博客
create database wordpress; grant all on wordpress.* to wordpress@'localhost' identified by '123456'; show grants for wordpress@'localhost'\G; # 如果授权授大了,在user表中删除用户是不行的,得drop然后重新授权 drop user wordpress@'localhost'; # revoke貌似不太好使 revoke all on *.* from wordpress@localhost; wget https://cn.wordpress.org/wordpress-4.5.1-zh_CN.tar.gz cp -a wordpress/* /application/nginx/html/blog/ # 这个权限是为了让先能用,以后还要改 chown -R www.www /application/nginx/html/blog/ # 现在blog目录下的文件有22个,安装后会多出一个配置文件 # 访问blog.etiantian.com,点击安装,会生成一个连接数据库的配置文件--wp-config.php # 图片上传目录为:/application/nginx/html/blog/wp-content/uploads
5.分离数据库
# 导出wordpress数据并修改连接数据库的文件(web01上) vi wp-config.php define('DB_HOST', '172.16.1.51'); mysqldump -uroot -poldboy123456 wordpress -B | gzip > bak.sql.gz # 导入数据(db01上) mysql -uroot -poldboy123456 < /tmp/bak.sql grant all on wordpress.* to wordpress@'172.16.1.%' identified by '123456'; # 在其他服务器上就能以这样的语句访问51的数据库了 mysql -u wordpress -h 172.16.1.51 -p
6.静态图片放在nfs上
# 10.0.0.31是之前的NFS服务器,在10.0.0.31上创建用户,与10.0.0.8上的www的id相同 useradd -u 891 www vi /etc/exports /data 172.16.1.0/24(rw,sync,all_squash,root_squash,anonuid=891,anongid=891) mkdir /data/nfs-blog chown -R www.www /data/ # 在web01上操作 rpm -qa rpcbind nfs-utils /etc/init.d/rpcbind status cd /application/nginx/html/blog/wp-content/uploads mv 2018 /tmp/ mount -t nfs 172.16.1.31:/data/nfs-blog /application/nginx/html/blog/wp-content/uploads/ cp -a /tmp/2018/ . 排错: touch: cannot touch `aaa': Permission denied # 挂载完之后,无法创建文件,是因为在/etc/exports中将anonuid写成了893, # 在改成891之后,还是无法写入,重启客户端rpcbind之后,可以正常写入.
7.wordpress实现伪静态
管理站点-设置-固定链接-自定义结构: /archives/%post_id%.html 文章的url就会变成这样的形式: http://blog.etiantian.com/archives/9.html cat blog.conf server { listen 80; server_name blog.etiantian.com; location / { root html/blog; index index.php index.html; try_files $uri $uri/ /index.php?$args; } location ~ .*\.(php|php5)?$ { root html/blog; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } }