LNMP环境搭建(CentOS 6.5 虚拟机中 )-详细

(未完结)

一. 安装包准备

搭建LNMP环境,首先就要准备好安装包(可以直接下载到真机桌面上):

  1. libiconv安装包:http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz
  2. mysql安装包: https://downloads.mysql.com/archives/community/(我下载的是mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz)
  3. PHP安装包:http://cn2.php.net/get/php-5.3.29.tar.gz/from/this/mirror
  4. Nginx安装包:http://nginx.org/en/download.html(我下载的是1.10.3版本)

然后选中安装包复制,粘贴到虚拟机的桌面即可。
注意:如果是刚刚安装的VMware,刚刚建立的虚拟机,可能会出现很多问题
不要急,一步一步解决:

  1. 复制安装包之后,粘贴不到虚拟机上
    解决方法:安装VMware Tools
    链接指路:https://jingyan.baidu.com/article/da1091fb3e618a027849d683.html
  2. 上述安装VMware Tools中,注意在sudo(也就是第8步执行安装)可能会出现错误:is not in sudoer file,意思就是当前的用户权限不够
    解决方案:要修改用户的权限
    ① 切换到root用户,输入su -,注意空格和 ‘ - ’
    ②输入visudo,回车,会出现很长的一堆信息
    ③移动光标,找到root ALL=(ALL)ALL这一行
    ④移动到这行下面,按a进入编辑模式
    ⑤输入auser ALL=(ALL)ALL
    ⑥按Esc,进入命令模式
    ⑦按Shift+;(分号),进入末行模式
    ⑧在最后一行输入w,保存修改
    ⑨再次进入末行模式,输入q退出
    ⑩修改完成,输入exit,退出root用户

二. 创建文件夹

  1. 创建服务和软件的专用文件夹
    输入:mkdir /data/{server,soft} -p
    (这里要加上-p,这样若有目录不存在会自动帮忙创建)
    当然,你也可以在自己指定的目录下创建。
  2. 移动安装包到创建的soft目录中
    输入:mv /home/auser/Desktop/* /data/soft
    (注意根据自己实际的目录,输入路径,若是桌面上还有其他东西且不想移动,就不要用 * 了)

三. 安装Nignx

先说正确的步骤(前提是需要的其他东西都有,否则参考后面的错误):

  • 打开Terminal终端,输入 su ,回车,输密码,使用root权限进行操作
  • 为Nignx创建一个使用用户,输入:useradd wwn -s /sbin/nologin -M
    (这一步是因为如果使用登陆的用户使用的话,该应用的权限过高,其中wwn 为所要创建的用户名称)
  • 输入:cd /data/soft,进入目录
  • 输入:tar xzf nginx-1.10.3.tar.gz,解压安装包
  • 输入:cd nginx-1.10.3,打开安装包
  • 输入:./configure --prefix=/data/server/nginx,进行配置
  • 输入:make,进行编译
  • 输入:make install,进行安装
  • 输入:gedit /data/server/nginx/conf/nginx.conf,修改配置文件,也就是分配之前创建的用户(注意可能无反应,等一会,或者重新执行命令)
  • 在打开的文件中找到第一行,“#user nobody;
  • 修改为“user wws;”,也就是去掉#号,nobody改为之前创建的用户名
  • 保存退出文件
  • 输入: /data/server/nginx/sbin/nginx,启动Nginx
  • 若无报错,则完成安装。想要查看当前Nginx提供的服务是否可行,可以在浏览器的地址栏,输入localhost,回车,若出现下面提示,则正确。
    LNMP环境搭建(CentOS 6.5 虚拟机中 )-详细

问题整理

./configure
之后报错:./configure: error: C compiler cc is not found
LNMP环境搭建(CentOS 6.5 虚拟机中 )-详细
解决方法
执行命令:
yum -y install gcc gcc-c++ autoconf automake make
LNMP环境搭建(CentOS 6.5 虚拟机中 )-详细
成功之后,再次尝试安装
又出现错误:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
解决方案
安装 pcre-devel 与 openssl-devel
yum -y install pcre-devel openssl openssl-devel
LNMP环境搭建(CentOS 6.5 虚拟机中 )-详细
成功后再次尝试安装nignx:
成功,无报错
LNMP环境搭建(CentOS 6.5 虚拟机中 )-详细

四. 安装mysql

注意同样是在root权限下操作
同样的,先来说说正确步骤:

  • 创建一个使用mysql的用户,名字就叫mysql
    输入:useradd -s /sbin/nologin -M mysql
  • 解压安装包到指定位置
    输入:tar xzf mysql-5.6.35.tar.gz -C /data/server
  • 为mysql-5.6.35 创建一个软链接,也就是快捷方式
    输入:ln -s mysql-5.6.35 mysql
  • 初始化数据库
    输入:/data/server/mysql/scripts/mysql_install_db --basedir=/data/server/mysql --datadir=/data/server/mysql/data/ --user=mysql
  • 数据库配置文件管理
    输入:
    mv /etc/my.cnf /etc/my.cnf-bak
    cp /data/server/mysql/support-files/my-default.cnf /etc/my.cnf
    (注意上述的两个命令中的两个路径之间的空格,****显示的不明显)
  • 数据库启动命令配置
    输入:
    cp /data/server/mysql/support-files/mysql.server /etc/init.d/mysqld
  • 修改启动文件
    输入:sed -i ’ s#/usr/local/mysql#/data/server/mysql#g’ /data/server/mysql/bin/mysqld_safe /etc/init.d/mysqld
    (命令的大概意思就是mysql数据库的服务由mysqld来管理,并且修改了配置文件)
  • 数据库文件权限设置
    输入:chown -R mysql.mysql /data/server/mysql/
  • 将数据库服务设置成开机自启动服务
    输入:
    chkconfig --add mysqld
    chkconfig mysqld on
    (这里的mysqld就是之前设置的新的数据库服务mysqld)
  • 数据库启动
    输入:service mysqld start,出现OK,或者success即可
    (目前要想进入数据库,还是需要找到mysql的路径下的bin的mysql可执行程序,然后输入./mysql -uroot,很不方便,所以需要下一步。)
  • 配置环境变量,也就是路径
    输入:
    gedit /etc/profile,进入配置文件
    在最后新输入一行:PATH=/data/server/mysql/bin:$PATH
    保存,退出
    输入:source /etc/profile,运行一下文件
    完成,在任何目录下输入 mysql -uroot都可进入数据库
    LNMP环境搭建(CentOS 6.5 虚拟机中 )-详细

问题整理

①在初始化数据库时,出现找不到文件
解决方案:发现时最开始下载的数据库的安装包有问题,所以重新下载了一下
②删除目录时,需要确认每一个文件
解决方法:使用rm -rf的命令
-r 就是向下递归,不管有多少级目录,一并删除
-f 就是直接强行删除,不作任何提示的意思
③注意输入目录时不要输错

五. 安装PHP

正确的安装步骤:

  • 首先安装依赖软件libiconv
    输入:
    cd /data/soft*
    tar xzf libiconv-1.14.tar.gz
    ./configure --prefix=/usr/local/libiconv
    make
    make install
  • 解压PHP
    输入:
    cd /data/soft*
    tar xzf php.5.3.29.tar.gz
    cd php.5.3.29
  • 配置
    输入:
    ln -s /data/server/mysql/lib/libmysqlclient.so.18 /usr/lib64/
    上述命令这是创建软链接
    touch ext/phar/phar.phar
    上述命令这是创建文件
    ./configure --prefix=/data/server/php-5.3.29 --with-mysql=/data/server/mysql --with-pdo-mysql=mysqlnd --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-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --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-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp
    上述命令是configure命令,如果没错的话,截图如下:
    LNMP环境搭建(CentOS 6.5 虚拟机中 )-详细
    如果有错的话,参考之后的问题总结(我的错误还挺多的,主要是虚拟机刚创建的,许多东西没有安装)
  • 编译,输入:make,时间较长,耐心等待
  • 安装,输入:make install
  • 修改php.ini文件
    输入:
    cp /data/soft/php-5.3.29/php.ini-production /data/server/php/lib/php.ini
    gedit /data/server/php/lib/php.ini
    如下图所示:
    LNMP环境搭建(CentOS 6.5 虚拟机中 )-详细
  • 找到**session.save_path=“tmp”
  • 将分号去掉
    LNMP环境搭建(CentOS 6.5 虚拟机中 )-详细
  • 备份php文件
    输入:
    cp /data/server/php/etc/php-fpm.conf.default /data/server/php/etc/php-fpm.conf
  • 启动php
    输入:/data/server/php/sbin/php-fpm
    (若报错,参考之后的问题总结)
  • 若要关闭php,因为没有对应的命令,可以暴力杀死

问题总结

以下都是configure的时候出现的问题,解决之后,记得重新configure一下
configure: error: xml2-config not found. Please check your libxml2 installation
解决方案:链接指路:https://blog.****.net/qq_35202206/article/details/81125884
configure: error: Please reinstall the libcurl
解决方案:链接指路:https://blog.****.net/my_one_piece/article/details/72866018
configure: error: jpeglib.h not found.
解决方案:链接指路:https://blog.****.net/blueberry521/article/details/79397958
configure: error: png.h not found.
解决方案:链接指路:https://blog.****.net/lovely_1014/article/details/54023976
Configure: error: freetype.h not found
解决方案:链接指路:https://blog.****.net/u010098331/article/details/50946730
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决方案:链接指路:https://blog.****.net/tanga842428/article/details/76861462
configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
解决方案:链接指路:https://blog.****.net/weixin_34414650/article/details/91503510
Cannot retrieve metalink for repository: epel. Please verify its path and try again
解决方案:链接指路:https://blog.****.net/awhane/article/details/42524653
Cannot retrieve repository metadata (repomd.xml) for repository: epel.
解决方案:链接指路:https://blog.****.net/qq_39590773/article/details/89790154
Error downloading packages
解决方案:链接指路:https://blog.****.net/github_38336924/article/details/89328836

以下是启动php报的错误
[25-Mar-2020 09:57:52] ERROR: [pool www] cannot get uid for user ‘www’
[25-Mar-2020 09:57:52] ERROR: FPM initialization failed

解决方案:大概是说php-fpm.conf文件中的配置不对,user和group的问题,
找到安装目录下etc子目录里的php-fpm.conf文件,打开
找到user和group,这里应该是‘www’,改成‘wwn’,这个用户是之前为Nginx创建的,目前没有出错,出错了再说。