LNMP架构搭建
系统:
CentOS6.5
1.安装Nginx
#安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
#安装PCRE让 Nginx支持Rewrite功能
wget https://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35;./configure;make && make install
pcre-config --version
#安装ngx_cache_purge-2.0
mkdir -p /data/software
cd /data/software/
wget http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz
tar zvfx ngx_cache_purge-2.0.tar.gz
#安装GD库
yum install gd-devel
yum -y install perl-devel perl-ExtUtils-Embed #
#安装/data/webserver/nginx
cd /usr/local/src/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zvxf nginx-1.5.2.tar.gz
cd nginx-1.5.2
./configure --user=www --group=www --prefix=/data/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_mp4_module --with-http_flv_module --with-http_perl_module --add-module=/data/software/ngx_cache_purge-2.0 --with-pcre=/usr/local/src/pcre-8.35
make&&make install
/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www
/data/webserver/nginx/sbin/nginx -v #查看版本
/data/webserver/nginx/sbin/nginx -t #测试
/data/webserver/nginx/sbin/nginx #启动
#访问服务器成功
#安装/data/webserver/nginx2
#第一个nginx作为web cache,能够缓存静态文件,第二个nginx作为server处理动态请求。
/usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx,不会重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
2.编译安装Mysql5.6.26
# 安装依赖
yum -y install wget lrzsz
yum -y install screen make zip unzip
yum -y install gcc gcc-c++
yum -y install gdb
yum -y install bison bison-devel
yum -y install ncurses-devel
yum -y install cmake
yum -y install perl perl-CPAN
yum -y install mysql-devel
yum -y install bison
yum -y install libaio
yum -y install cmake
# 安装pcre库
tar zxf pcre-8.35.tar.gz
cd pcre-8.35/
./configure
make && make install
groupadd -g 501 mysql
useradd -u 501 -g mysql mysql
mkdir -p /home/mysql/mysql/{tmp,etc,log}
chmod 777 /home/mysql/mysql/tmp
cd /root/5.6.26/software/mysql-5.6.26
/sbin/ldconfig
cmake -DCMAKE_INSTALL_PREFIX=/home/mysql/mysql -DMYSQL_UNIX_ADDR=/home/mysql/mysql/tmp/mysql.sock -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_EXTRA_CHARSETS:STRING=binary,utf8mb4,gbk,gb2312,utf8,latin1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/home/mysql/mysql/var -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=5858 -DSYSCONFDIR=/home/mysql/mysql/etc -DINSTALL_SHAREDIR=share -DMYSQL_USER=mysql -DWITH_DEBUG=0
cd /root/5.6.26/software/mysql-5.6.26/source_downloads/gmock-1.6.0
./configure --prefix=/home/mysql/mysql
make
cd /root/5.6.26/software/mysql-5.6.26
make -j 4
make install
ln -s /home/mysql/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
cp /home/mysql/mysql/support-files/mysql.server /home/mysql/mysql/bin
/home/mysql/mysql/scripts/mysql_install_db --basedir=/home/mysql/mysql --datadir=/home/mysql/mysql/var --user=mysql
rm -rf /etc/my.cnf
chown -R mysql:mysql /home/mysql/mysql
chown -R mysql:mysql /home/mysql/mysql/var
/bin/cp -rp /root/5.6.26/conf/mysql_conf/* /home/mysql/mysql/etc/
3.编译安装 php 5.4.26
yum install libxml2 libxml2-devel
yum install bzip2 bzip2-devel
yum -y install curl-devel
yum install openldap openldap-devel -y
cp -frp /usr/lib64/libldap* /usr/lib/
yum install -y libmcrypt-devel
wget https://www.php.net/distributions/php-5.4.26.tar.gz
cd php-5.4.26
./configure --prefix=/data/webserver/php --with-config-file-path=/data/webserver/php/etc --with-mysql=/home/mysql/mysql --with-mysqli=/home/mysql/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir=/usr/lib64 --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --enable-pdo --with-gettext --with-pdo-mysql --with-bz2
make&&make install
#(加的参数有不支持的,提示比如:configure: WARNING: unrecognized options: --enable-discard-path, --enable-safe-mode, --enable-fastcgi, --enable- force-cgi-redirect说明上述这些参数废弃了)
vim /etc/php.ini #阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击。修改cgi.fix_pathinfo=0
vim /data/webserver/php/etc/php-fpm.conf #修改user = www; group = www
/data/webserver/php/sbin/php-fpm #启动
4.配置 Nginx 使其支持 PHP 应用:
vim /data/webserver/nginx/conf/nginx.conf
#修改默认的 location 块,使其支持 .php 文件:
location / {
root html;
index index.php index.html index.htm;
}
#下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
#Nginx重新载入配置。
/data/webserver/nginx/sbin/nginx -s reload
#创建测试文件
rm -f /data/webserver/nginx/html/index.html
echo "<?php phpinfo(); ?>" > /data/webserver/nginx/html/index.php
#打开浏览器,访问 http://http://172.17.1.119,将会显示 phpinfo()
遇到问题:
1.修改ssh端口后,Connection timed out
#因为防火墙没有通过端口10033的访问
vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 10033 -j ACCEPT
service iptables restart
ssh 172.17.1.119 -p 10033
#连接成功
2. rz: command not found
yum install lrzsz
3.CMakeLists.txt" does not match the source “/home/wzy/caffe/CMakeLists.txt” used to generate cache. Re-run cmake with a different source directory.
#进入build目录,删除CMakeCache.txt即可
4./home/mysql/mysql/bin/mysql.server start->ERROR! The server quit without updating PID file (/home/mysql/mysql/var/mysql.pid).
df -h #发现/home分区用量满了
du -sh * #找到大文件/home/mysql/mysql/var/{ib_logfile101,ib_logfile1}
#检查配置文件
vim /home/mysql/mysql/etc/my.cnf
#将存储数据的目录修改成/data/mysql
5.File ‘./mysql-bin.index’ not found (Errcode: 13 - Permission denied)
chown -R mysql:mysql /data/mysql
chown -R mysql:mysql /data/mysql/var
6.[ERROR] Fatal error: Can’t open and lock privilege tables: Table ‘mysql.user’ doesn’t exist
/home/mysql/mysql/scripts/mysql_install_db --basedir=/home/mysql/mysql --datadir=/data/mysql/var --user=mysql
#成功启动
/home/mysql/mysql/bin/mysql.server start
#->Starting MySQL. SUCCESS!
7.ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
#在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程
#成功登录
#修改root的密码
mysql> use mysql;
mysql> update user set password=password("123456") where user="root";
mysql> flush privileges;
mysql> quit
#root账户就已经重置成新的密码了
#编辑my.cnf,去掉刚才添加的内容,然后重启MySQL
8.php测试文件403 Forbidden
#原因:修改nginx.conf添加index.php后reopen重启命令有问题
/data/webserver/nginx/sbin/nginx -s stop
/data/webserver/nginx/sbin/nginx
9.[error] 437#0: *1 connect() failed (111: Connection refused)
netstat -lntup #发现没有9000端口
#原来之前kill了php忘记重开了
#重启后成功访问

本文详细介绍了在CentOS6.5环境下从零开始搭建LNMP(Linux+Nginx+MySQL+PHP)架构的全过程,包括Nginx、MySQL和PHP的安装与配置,以及解决过程中可能遇到的问题。
2万+

被折叠的 条评论
为什么被折叠?



