使用LNMP部署静态网站(一)

使用LNMP部署静态网站(一)

LNMP动态网站部署架构是一套由Linux + Nginx + MySQL + PHP组成的动态网站系统解决方案(其logo见图20-1)。LNMP中的字母L是Linux系统的意思,不仅可以是RHEL、CentOS、Fedora,还可以是Debian、Ubuntu等系统。
1.在使用源码包安装服务程序之前,首先要让安装主机具备编译程序源码的环境,他需要具备C语言、C++语言、Perl语言的编译器,以及各种常见的编译支持函数库程序。因此请先配置妥当Yum软件仓库,然后把下面列出的这些软件包都统统安装上:
[root@linuxprobe ~]# yum install -y apr autoconf automake bison bzip2 bzip2 compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc+±devel libtool* libgomp libxml2 libxml2-devel libXpm* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel**

把安装LNMP动态网站部署架构所需的16个软件源码包和1个用于检查效果的论坛网站系统软件包上传到打算部署LNMP动态网站架构的Linux服务器中:
[root@linuxprobe ~]# cd /usr/local/src
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/cmake-2.8.11.2.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/Discuz_X3.2_SC_GBK.zip
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/freetype-2.5.3.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/jpegsrc.v9a.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/libgd-2.1.0.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/libmcrypt-2.5.8.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/libpng-1.6.12.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/libvpx-v1.3.0.tar.bz2
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/mysql-5.6.19.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/nginx-1.6.0.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/openssl-1.0.1h.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/php-5.5.14.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/pcre-8.35.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/t1lib-5.1.2.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/tiff-4.0.3.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/yasm-1.2.0.tar.gz
[root@linuxprobe src] # wget https://www.linuxprobe.com/Software/zlib-1.2.8.tar.gz
[root@linuxprobe src]# ls
zlib-1.2.8.tar.gz libmcrypt-2.5.8.tar.gz pcre-8.35.tar.gz
cmake-2.8.11.2.tar.gz libpng-1.6.12.tar.gz php-5.5.14.tar.gz
Discuz_X3.2_SC_GBK.zip libvpx-v1.3.0.tar.bz2 t1lib-5.1.2.tar.gz
freetype-2.5.3.tar.gz mysql-5.6.19.tar.gz tiff-4.0.3.tar.gz
jpegsrc.v9a.tar.gz nginx-1.6.0.tar.gz yasm-1.2.0.tar.gz
libgd-2.1.0.tar.gz openssl-1.0.1h.tar.gz

CMake是Linux系统中一款常用的编译工具。要想通过源码包安装服务程序,就一定要严格遵守上面总结的安装步骤—下载及解压源码包文件、编译源码包代码、生成二进制安装程序、运行二进制的服务程序安装包。接下来在解压、编译各个软件包源码程序时,都会生成大量的输出信息,下文中将其省略,请读者以实际操作为准。
[root@linuxprobe src]# tar xzvf cmake-2.8.11.2.tar.gz
[root@linuxprobe src]# cd cmake-2.8.11.2/
[root@linuxprobe cmake-2.8.11.2]# ./configure
[root@linuxprobe cmake-2.8.11.2]# make
[root@linuxprobe cmake-2.8.11.2]# make install

2.配置Mysql服务
[root@linuxprobe cmake-2.8.11.2]# cd …
[root@linuxprobe src]# useradd mysql -s /sbin/nologin

创建一个用于保存MySQL数据库程序和数据库文件的目录,并把该目录的所有者和所属组身份修改为mysql。其中,/usr/local/mysql是用于保存MySQL数据库服务程序的目录,/usr/local/mysql/var则是用于保存真实数据库文件的目录。
[root@linuxprobe src]# mkdir -p /usr/local/mysql/var
[root@linuxprobe src]# chown -Rf mysql:mysql /usr/local/mysql

接下来解压、编译、安装MySQL数据库服务程序。在编译数据库时使用的是cmake命令,其中,-DCMAKE_INSTALL_PREFIX参数用于定义数据库服务程序的保存目录,-DMYSQL_DATADIR参数用于定义真实数据库文件的目录,-DSYSCONFDIR则是定义MySQL数据库配置文件的保存目录。
[root@linuxprobe src]# tar xzvf mysql-5.6.19.tar.gz
[root@linuxprobe src]# cd mysql-5.6.19/
[root@linuxprobe mysql-5.6.19]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc
[root@linuxprobe mysql-5.6.19]# make
[root@linuxprobe mysql-5.6.19]# make install

为了让MySQL数据库程序正常运转起来,需要先删除/etc目录中的默认配置文件,然后在MySQL数据库程序的保存目录scripts内找到一个名为mysql_install_db的脚本程序,执行这个脚本程序并使用–user参数指定MySQL服务的对应账号名称(在前面步骤已经创建),使用–basedir参数指定MySQL服务程序的保存目录,使用–datadir参数指定MySQL真实数据库的文件保存目录,这样即可生成系统数据库文件,也会生成出新的MySQL服务配置文件。
[root@linuxprobe mysql-5.6.19]# rm -rf /etc/my.cnf
[root@linuxprobe mysql-5.6.19]# cd /usr/local/mysql
[root@linuxprobe mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var

把系统新生成的MySQL数据库配置文件链接到/etc目录中,然后把程序目录中的开机程序文件复制到/etc/rc.d/init.d目录中,以便通过service命令来管理MySQL数据库服务程序。记得把数据库脚本文件的权限修改成755以便于让用户有执行该脚本的权限:
[root@linuxprobe mysql]# ln -s my.cnf /etc/my.cnf
[root@linuxprobe mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@linuxprobe mysql]# chmod 755 /etc/rc.d/init.d/mysqld

编辑刚复制的MySQL数据库脚本文件,把第46、47行的basedir与datadir参数分别修改为MySQL数据库程序的保存目录和真实数据库的文件内容。
[root@linuxprobe mysql]# vim /etc/rc.d/init.d/mysqld
………………省略部分输出信息………………
39 #
40 # If you want to affect other MySQL variables, you should make your changes
41 # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
42
43 # If you change base dir, you must also change datadir. These may get
44 # overwritten by settings in the MySQL configuration files.
45
46 basedir=/usr/local/mysql
47 datadir=/usr/local/mysql/var
48
………………省略部分输出信息………………

配置好脚本文件后便可以用service命令启动mysqld数据库服务了。mysqld是MySQL数据库程序的服务名称,注意不要写错。顺带再使用chkconfig命令把mysqld服务程序加入到开机启动项中。
[root@Linuxprobe mysql]# service mysqld start
Starting MySQL. SUCCESS!
[root@linuxprobe mysql]# chkconfig mysqld on

MySQL数据库程序自带了许多命令,但是Bash终端的PATH变量并不会包含这些命令所存放的目录,因此我们也无法顺利地对MySQL数据库进行初始化,也就不能使用MySQL数据库自带的命令了。想要把命令所保存的目录永久性地定义到PATH变量中,需要编辑/etc/profile文件并写入追加的命令目录,这样当物理设备在下一次重启时就会永久生效了。如果不想通过重启设备的方式来生效,也可以使用source命令加载一下/ect/profile文件,此时新的PATH变量也可以立即生效了。
[root@linuxprobe mysql]# vim /etc/profile
………………省略部分输出信息………………
64
65 for i in /etc/profile.d/
.sh ; do
66 if [ -r " i " ] ; t h e n 67 i f [ " i" ]; then 67 if [ " i"];then67if["{-#i}" != “ − " ] ; t h e n 68. " -" ]; then 68 . " "];then68."i”
69 else
70 . " i " > / d e v / n u l l 71 f i 72 f i 73 d o n e 74 e x p o r t P A T H = i" >/dev/null 71 fi 72 fi 73 done 74 export PATH= i">/dev/null71fi72fi73done74exportPATH=PATH:/usr/local/mysql/bin
75 unset i
76 unset -f pathmunge
[root@linuxprobe mysql]# source /etc/profile

MySQL数据库服务程序还会调用到一些程序文件和函数库文件。由于当前是通过源码包方式安装MySQL数据库,因此现在也必须以手动方式把这些文件链接过来。
[root@linuxprobe mysql]# mkdir /var/lib/mysql
[root@linuxprobe mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
[root@linuxprobe mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
[root@linuxprobe mysql]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql

现在,MySQL数据库服务程序已经启动,调用的各个函数文件已经就位,PATH环境变量中也加入了MySQL数据库命令的所在目录。接下来准备对MySQL数据库进行初始化,这个初始化的配置过程与MariaDB数据库是一样的,只是最后变成了Thanks for using MySQL!
**[root@linuxprobe mysql]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we’ll need the current
password for the root user. If you’ve just installed MySQL, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): 此处只需按下回车键
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y (要为root管理员设置数据库的密码)
New password: 输入要为root管理员设置的数据库密码
Re-enter new password: 再输入一次密码
Password updated successfully!
Reloading privilege tables…
… Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y (删除匿名账户)
… Success!
Normally, root should only be allowed to connect from ‘localhost’. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y (禁止root管理员从远程登录)
… Success!
By default, MySQL comes with a database named ‘test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y (删除test数据库并取消对其的访问权限)

  • Dropping test database…
    … Success!
  • Removing privileges on test database…
    … Success!
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    Reload privilege tables now? [Y/n] y (刷新授权表,让初始化后的设定立即生效)
    … Success!
    All done! If you’ve completed all of the above steps, your MySQL
    installation should now be secure.
    Thanks for using MySQL!
    Cleaning up…**

你可能感兴趣的:(使用LNMP部署静态网站(一))