搭建LNMP;搭建WIKI

时间:2022-01-08 15:50:27

#!/bin/bash
#lnmp搭建
#搭建WIKI

1.系统检测,系统环境优化

搭建版本:

nginx-1.8.1.tar.gz
mysql-5.5.32-linux2.6-x86_64.tar.gz
libmcrypt-2.5.8.tar.gz
mhash-0.9.9.9.tar.gz
mcrypt-2.6.8.tar.gz
php-5.3.27.tar.gz

更新yum源:cd /etc/yum.repos.d/
mv CentOS6-Base-163.repo CentOS6-Base-163.repo.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum clean all
yum makecache
检查系统版本信息:
uname -r
2.6.32-504.el6.x86_64
cat /etc/redhat-release
CentOS release 6.6 (Final)
关闭selinux关闭防火墙:
getenforce
Disabled
iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

2.安装nginx

mkdir ~/tools
cd ~/tools
wget http://nginx.org/download/nginx-1.8.1.tar.gz
yum install pcre pcre-devel openssl openssl-devel -y
yum install zlib zlib-devel gcc gcc-c++ autoconf automake
useradd -s /sbin/nologin nginx -M

tar xf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module

make && make install
echo $?

ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
#启动:
nginx
netstat -lntup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3420/nginx
#重启:
nginx -s reload
#停止
#查询nginx进程号
ps -ef|grep nginx
#停止进程
kill -QUIT 主进程号
#快速停止
kill -TERM 主进程号
#强制停止
pkill -9 nginx
访问验证:
http://192.168.146.134/

3.安装MySQL

选择版本下载:https://downloads.mysql.com/archives/community/
wget https://downloads.mysql.com/archives/get/file/mysql-5.5.32-linux2.6-x86_64.tar.gz
tar zxvf mysql-5.5.32-linux2.6-x86_64.tar.gz
mv mysql-5.5.32-linux2.6-x86_64 /usr/local/mysql-5.5.32
ln -s /usr/local/mysql-5.5.32 /usr/local/mysql
useradd -s /sbin/nologin -M mysql
mkdir /mysql/data -p
chown -R mysql.mysql /mysql/data
cd /usr/local/mysql
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mysql/data/
echo $?
cp support-files/my-small.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
#修改启动脚本路径
sed -i 's#^basedir=#basedir=/usr/local/mysql#g' /etc/init.d/mysqld
sed -i 's#^datadir=#basedir=/mysql/data#g' /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
#启动关闭MySQL
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
#方法2:
/usr/local/mysql/bin/msyql_safe & #后台启动
mysqladmin shutdown #优雅关闭MySQL服务
#查看运行状态
netstat -lntup|grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 33369/mysqld
lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 33369 mysql 10u IPv4 53889 0t0 TCP *:mysql (LISTEN)

#添加系统自启动
chkconfig --add mysqld
chkconfig --level 345 mysqld on
#添加环境变量
echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
source /etc/profile

#修改初始密码
方法1:mysqladmin -uroot password '123.com'
方法2:
update user set password=password('456.com') where user='root' and host='localhost';
flush privileges;
方法3:
set password=password('123.com');
flush privileges;

#优化数据库
mysql -uroot -p

drop database test;
select user,host from mysql.user;
+------+-----------------+
| user | host |
+------+-----------------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | zhang.python.cn |
| root | zhang.python.cn |
+------+-----------------+
6 rows in set (0.21 sec)

drop user ''@'zhang.python.cn';
drop user ''@'localhost';
drop user 'root'@'::1';
drop user 'root'@'zhang.python.cn';
select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)

#注意:grant新建的用户drop删除不了,需要用delete删除。
grant all on *.* to root@'a' identified by '123.com';
delete from mysql.user where user='root' and host='a';

4.安装PHP

安装前确保nginx和MySQL启动正常:
netstat -lntup|egrep 'nginx|mysql'
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 33369/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3420/nginx

检查系统是否有以下包文件:
rpm -qa zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel
yum -y install zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel
rpm -qa zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel
libpng-1.2.49-2.el6_7.x86_64
gd-devel-2.0.35-11.el6.x86_64
libxml2-devel-2.7.6-21.el6_8.1.x86_64
freetype-devel-2.3.11-17.el6.x86_64
zlib-devel-1.2.3-29.el6.x86_64
libpng-devel-1.2.49-2.el6_7.x86_64
gd-2.0.35-11.el6.x86_64
zlib-1.2.3-29.el6.x86_64
freetype-2.3.11-17.el6.x86_64
curl-7.19.7-52.el6.x86_64

#安装libiconv字符集转换包:
下载:wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make && make install
cd ..

#安装libmcrypt加密库:
wget https://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make
make install
sleep 2
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
echo $?
cd ../../

#安装mhash加密扩展库

wget https://jaist.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz

tar zxf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make
make install
cd ../
rm -f /usr/lib64/libmcrypt.*
rm -f /usr/lib64/libmhash*
ln -s /usr/local/lib64/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib64/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib64/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib64/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib64/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib64/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib64/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib64/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib64/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

#安装mcrypt加密扩展库

wget https://jaist.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
tar zxf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
/sbin/ldconfig
./configure LD_LIBRARY_PATH=/usr/local/lib
make
make install
sleep 2
cd ..

#安装PHP
yum install libxslt* -y
wget http://219.238.7.66/files/7220000008ECCD21/in1.php.net/distributions/php-5.3.27.tar.gz
tar zxvf php-5.3.27.tar.gz
cd php-5.3.27

./configure \
> --prefix=/usr/local/php5.3.27 \
> --with-mysql=/usr/local/mysql \
> --with-iconv-dir=/usr/local/libiconv \
> --with-freetype-dir \
> --with-jpeg-dir \
> --with-png-dir \
> --with-zlib \
> --with-libxml-dir=/usr \
> --enable-xml \
> --disable-rpath \
> --enable-safe-mode \
> --enable-bcmath \
> --enable-shmop \
> --enable-sysvsem \
> --enable-inline-optimization \
> --with-curl \
> --with-curlwrappers \
> --enable-mbregex \
> --enable-fpm \
> --enable-mbstring \
> --with-mcrypt \
> --with-gd \
> --enable-gd-native-ttf \
> --with-openssl \
> --with-mhash \
> --enable-pcntl \
> --enable-sockets \
> --with-xmlrpc \
> --enable-zip \
> --enable-soap \
> --enable-short-tags \
> --enable-zend-multibyte \
> --enable-static \
> --with-xsl \
> --with-fpm-user=nginx \
> --with-fpm-group=nginx \
> --enable-ftp

echo $?
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
make
echo $?
make install
echo $?
ln -s /usr/local/php5.3.27/ /usr/local/php

#配置PHP
cp php.ini-production /usr/local/php/lib/php.ini

修改配置:
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf

[global]
pid = /app/logs/php-fpm.pid
error_log = /app/logs/php-fpm.log
log_level = error

rlimit_files = 32768
events.mechanism = epoll
[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000
listen.owner = nginx
listen.group = nginx

pm = dynamic
pm.max_children = 1024
pm.start_servers = 16
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.process_idle_timeout = 15s;
pm.max_requests = 2048

slowlog = /app/logs/$pool.log.slow
request_slowlog_timeout = 10

php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f 49000448@qq.com

#检查语法:
/usr/local/php/sbin/php-fpm -t
[30-Mar-2017 14:43:44] ERROR: failed to open error_log (/app/logs/php-fpm.log): No such file or directory (2)
[30-Mar-2017 14:43:44] ERROR: failed to post process the configuration
[30-Mar-2017 14:43:44] ERROR: FPM initialization failed

mkdir -p /app/logs
/usr/local/php/sbin/php-fpm -t
[30-Mar-2017 14:44:31] NOTICE: configuration file /usr/local/php5.3.27/etc/php-fpm.conf test is successful

#启动PHP-fpm
/usr/local/php/sbin/php-fpm
netstat -lntup|grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 61675/php-fpm

ps -ef|grep php-fpm
root 61675 1 0 14:45 ? 00:00:00 php-fpm: master process (/usr/local/php5.3.27/etc/php-fpm.conf)
nginx 61676 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61677 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61678 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61679 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61680 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61681 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61682 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61683 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61684 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61685 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61686 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61687 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61688 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61689 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61690 61675 0 14:45 ? 00:00:00 php-fpm: pool www
nginx 61691 61675 0 14:45 ? 00:00:00 php-fpm: pool www

#开机自动运行服务
vim /etc/rc.local
/etc/init.d/mysqld start
/usr/local/php/sbin/php-fpm
/usr/local/nginx/sbin/nginx

5.nginx整合PHP

#修改nginx配置文件:
vim /sur/local/nginx/conf/nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/bbs.conf; #为每个服务建立配置文件
include extra/blog.conf;
}

mkdir /usr/local/nginx/conf/extra
cd /usr/local/nginx/conf/extra
touch bbs.conf
touch www.conf
touch blog.conf

vim bbs.conf

server {
listen 80;
server_name bbs.zhangxinqi.com;
root html/bbs;
index index.html index.htm index.php;
access_log logs/bbs_access.log;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}

}

vim www.conf

server {
listen 80;
server_name www.zhangxinqi.com;
root html/www;
index index.html index.htm;
access_log logs/www_access.log;
location /status{
stub_status on;
access_log off;
}
}
#检查语法
nginx -t
#重启动nginx
nginx -s reload

#建立PHP测试文件
vim html/bbs/index.php
<?php
phpinfo();
?>

#测试能访问到php文件,就表示nginx会将php文件推送给php处理。
http://bbs.zhangxinqi.com

#测试MySQL链接是否正常
vim mysql_test.php
<?php
//$link_id=mysql_connect('主机名','用户','密码');
$link_id=mysql_connect('localhost','root','123.com') or mysql_error();
if($link_id){
echo "mysql successful by zhang\n";
}else{
echo "mysql_error()";
}
?>
#访问输出结果为“mysql successful by zhang"则为正常
http://bbs.zhangxinqi.com/mysql_test
#系统下以PHP命令测试
/usr/local/php/bin/php mysqltest.php
mysql successful by zhang
/etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS!
/usr/local/php/bin/php mysqltest.php
PHP Warning: mysql_connect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /usr/local/nginx/html/bbs/mysqltest.php on line 3
mysql_error()'
/etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
/usr/local/php/bin/php mysqltest.php
mysql successful by zhang

到此LNMP搭建全部完成!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

6.搭建WIKI

wiki下载:http://kaiyuan.hudong.com/download/

#建立数据库和用户授权:
create database wiki character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on wiki.* to 'wiki'@'localhost' identified by '123.com';
Query OK, 0 rows affected (0.22 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

cd /usr/local/nginx/html/bbs
unzip HDWiki-v5.1UTF8-20141205博客站点.zip
rm -fr HDWiki-v5.1UTF8-20141205博客站点.zip
mv HDWiki-v5.1UTF8-20121102/* .
mv document/* .
mv hdwiki/* .

http://bbs.zhangxinqi.com
chown -R nginx.nginx bbs/

完成WIKI搭建。。。。。。。。。。。。。。。。。。。。。