Linux(CentOS 7)+ Nginx(1.10.2)+ Mysql(5.7.16)+ PHP(7.0.12)
首先安装Linux系统,我以虚拟机安装来做示例,先去下载 VitualBox,这是一款开源的虚拟机软件,https://www.virtualbox.org 官网地址。或者是VMware,www.vmware.com,不过这个软件是收费的。当然同时还要去下载一个Linux镜像,我下载是CentOS 7系统,https://www.centos.org/download
下载好了之后打开虚拟机,我用的是VMware,选择创建自定义虚拟机:
继续下一步:
点击完成。
看到这个界面后,点击CD/DVD(IDE):
点击选择光盘镜像,把下载好的Centos 7 系统放进去:
接着回到上个页面,点击启动磁盘:
选择CD/DVD,然后点击重新启动:
可以看到已经载入镜像文件,选择第一个安装:
选择语言,继续,
这玩意得先点进去,然后保存下,
要上网的同志不要忘记开启网络了:
这个时候就开始安装了,安装的同时把root密码和用户账号密码设置下:
设置完成后,我们就可以耐心的等待了……
OK,重启系统,登录root账户,也可以登录你自己设置好的用户账户,是不是很酷炫。
先看下网络有没有问题,输入ping www.baidu.com,看到网络可以正常访问:
如果不能正常访问网络,修改ifcfg文件,把ONBOOT="no" 改为 "yes",保存即可。
[root@172 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eno16777736
重启下网络:
systemctl restart network.service
查看ip地址,输入ip addr,红色部分可以直接在浏览器里访问到,如下图:
ok,没有问题,接下来你就可以安装各种软件了……当然,我们先做正事,开始安装PHP,先去PHP官网上下载压缩包:
选择一个版本,然后复制链接地址,在命令行输入:
wget http://hk1.php.net/get/php-7.0.12.tar.gz/from/this/mirror
提示找不到wget命令,先下载wget:
yum install wget
安装完成后在执行:
wget http://hk1.php.net/get/php-7.0.12.tar.gz/from/this/mirror
看到已经下载好到目录下了:
接下来再解压,输入:
tar -zxvf mirror
解压好后再进入到 http://php.net/manual/zh/install.fpm.php 来安装php-fpm,因为现在的php还不能和nginx一起工作,只能和Apache工作,php-fpm是nginx和php的一个桥梁,所以
我们继续安装php-fpm。
先安装需要的编译工具 gcc,gcc++,libxml2-devel:
yum install gcc gcc-c++ libxml2-devel
进入到php目录下进行编译和安装:
cd php-7.0.12/ //进入php目录下 ./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7 --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --enable-fpm --enable-libxml //这里选择要安装的php目录,后面参数是开启php-fpm,支持mysql等 make //进行编译 make install //进行安装
安装完成后进入到安装目录下:
cd /usr/local/php7/lib/php
php安装完成。
mysql的安装
首先创建一个名为mysql且没有登录权限
的用户和一个名为mysql的用户组:
groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16.tar.gz
安装mysql需要的编译工具:
yum -y install cmake gcc-c++ ncurses-devel perl-Data-Dumper boost boost-doc boost-devel
进入到mysql目录下进行编译和安装:
cd /mysql-5.7.16
cmake -DCMAKE_STALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/mysql/data -DSYSCONFDIR=/etc -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DENABLED_DOWNLOADS=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled
执行上面的配置命令的结果如下图所示:
测试发现编译MySQL5.7以及更高的版本时,都需要下载并引用或者直接安装boost库,否则在执行cmake
命令时会报如下错误:
-- Running cmake version 2.8.11 -- Configuring with MAX_INDEXES = 64U -- SIZEOF_VOIDP 8 -- MySQL 5.7.16 [MySQL版本] -- Packaging as: mysql-5.7.16-Linux-x86_64 -- Looked for boost/version.hpp in and -- BOOST_INCLUDE_DIR BOOST_INCLUDE_DIR-NOTFOUND -- LOCAL_BOOST_DIR -- LOCAL_BOOST_ZIP -- Could not find (the correct version of) boost. [关键错误信息] -- MySQL currently requires boost_1_59_0 [解决办法] CMake Error at cmake/boost.cmake:76 (MESSAGE): [具体错误和解决方法] You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory> This CMake script will look for boost in <directory>. If it is not there, it will download and unpack it (in that directory) for you. If you are inside a firewall, you may need to use an http proxy: export http_proxy=http://example.com:80 Call Stack (most recent call first): cmake/boost.cmake:228 (COULD_NOT_FIND_BOOST) CMakeLists.txt:452 (INCLUDE) -- Configuring incomplete, errors occurred! See also "/mydata/mysql-5.7.16/CMakeFiles/CMakeOutput.log".
只要将http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz下载下来,上传到/usr/local/boost,在执行命令:
cmake -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
编译和安装:
make && make install
查看编译成功后的MySQL安装目录:
使用cd命令查看MySQL的安装目录/usr/local/mysql/
下面是否生成了相关目录文件(最重要的当然是bin、sbin和lib目录)。如果lib目录下面没有生成如图所示的.so动态库文件和.a静态库文件,那么说明安装不成功,需要重新编译安装。(即使成功了也可能会导致php进程无法找到mysql的相关库文件)。
然后把编译生成的my.cnf文件备份:
cp /etc/my.cnf /etc/my.cnf.bak
再修改my.cnf配置如下图:(具体路径根据你安装的目录为准)
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [client] port=3306 socket=/var/run/mysql/mysql.sock [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. user = mysql basedir = /usr/local/mysql datadir = /mydata/mysql/data port=3306 server-id = 1 socket=/var/run/mysql/mysql.sock character-set-server = utf8 log-error = /var/log/mysql/error.log pid-file = /var/log/mysql/mysql.pid general_log = 1 skip-name-resolve #skip-networking back_log = 300 max_connections = 1000 max_connect_errors = 6000 open_files_limit = 65535 table_open_cache = 128 max_allowed_packet = 4M binlog_cache_size = 1M max_heap_table_size = 8M tmp_table_size = 16M read_buffer_size = 2M read_rnd_buffer_size = 8M sort_buffer_size = 8M join_buffer_size = 28M key_buffer_size = 4M thread_cache_size = 8 query_cache_type = 1 query_cache_size = 8M query_cache_limit = 2M ft_min_word_len = 4 log_bin = mysql-bin binlog_format = mixed expire_logs_days = 30 performance_schema = 0 explicit_defaults_for_timestamp #lower_case_table_names = 1 myisam_sort_buffer_size = 8M myisam_repair_threads = 1 interactive_timeout = 28800 wait_timeout = 28800 # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M
将MySQL编译生成的bin目录添加到当前Linux系统的环境变量中:
echo -e ' export PATH=/usr/local/mysql/bin:$PATH ' >> /etc/profile && source /etc/profile
创建MySQL数据库文件的存放路径以及相关安全配置
在Linux主机上创建一个目录/mydata/mysql/data
,用于存放MySQL的数据库文件。同时设置其用户和用户组为之前创建的mysql
,权限为777。这样其它用户是无法进行读写的,尽量保证数据库的安全。
mkdir -p /mydata/mysql/data && chown -R root:mysql /usr/local/mysql chown -R mysql:mysql /mydata/mysql/data chmod -R go-rwx /mydata/mysql/data
初始化MySQL自身的数据库
在MySQL安装目录的in
路径下,执行mysqld命令,初始化MySQL自身的数据库。
cd /usr/local/mysql/bin mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/mysql/data
执行完后,通过 ls -lrt /mydata/mysql/data/
命令查看是否生成了MySQL自身的数据库文件。
创建MySQL日志存放目录以及设置开机启动
下面配置的MySQL日志存放目录以及权限都是根据前面my.cnf文件写的,也就是两者需要保持一致。
mkdir -p /var/run/mysql && mkdir -p /var/log/mysql chown -R mysql:mysql /var/log/mysql && chown -R mysql:mysql /var/run/mysql #配置开机自启动 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld #增加可执行权限 chkconfig --add mysqld #添加到sysV服务 chkconfig mysqld on
在完成上面的操作后,就可以正式使用MySQL服务了。启动MySQL进程服务的命令如下:
[root@172 ~]# mysqld_safe --user=mysql --datadir=/mydata/mysql/data --log-error=/var/log/mysql/error.log & [1] 19077 [root@172 ~]# 2016-10-23T04:21:19.530315Z mysqld_safe Logging to '/var/log/mysql/error.log'. 2016-10-23T04:21:19.563588Z mysqld_safe Starting mysqld daemon with databases from /mydata/mysql/data ######上面这条命令会在后台继续执行,所以直接回车并执行下面这条命令 [root@172 ~]# service mysqld start Starting MySQL SUCCESS!
使用 ps -ef | grep mysql 查看MYSQL服务端进程和端口监听情况:
[root@172 ~]# ps -ef | grep mysql root 19077 18966 0 12:21 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql --datadir=/mydata/mysql/data --log-error=/var/log/mysql/error.log mysql 19685 19077 0 12:21 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mydata/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql/error.log --open-files-limit=65535 --pid-file=/var/log/mysql/mysql.pid --socket=/var/run/mysql/mysql.sock --port=3306 root 20318 18966 0 12:22 pts/0 00:00:00 grep --color=auto mysql
初始化MySQL数据库的root用户密码
和Oracle数据库一样,MySQL数据库也默认自带了一个root
用户(这个和当前Linux主机上的root用户是完全不搭边的),我们在设置好MySQL数据库的安全配置后初始化root用户的密码。配置过程中,一路输入y
就行了。这里只说明下MySQL5.7.16版本中,用户密码策略分成低级LOW
、中等MEDIUM
和超强STRONG
三种,推荐使用中等MEDIUM
级别!
[root@172 ~]# mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: y There are three levels of password validation policy: LOW Length >= 8 【只需要长度大于或等于8】 MEDIUM Length >= 8, numeric, mixed case, and special characters 【还需要包含数字、大小写和类似于@#%等特殊字符】 STRONG Length >= 8, numeric, mixed case, special characters and dictionary file 【还需要包含字典文件】 Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: y
将MySQL数据库的动态链接库共享至系统链接库
一般MySQL数据库还会被类似于PHP等服务调用,所以我们需要将MySQL编译后的lib库文件添加至当前Linux主机链接库/etc/ld.so.conf.d/
下,这样MySQL服务就可以被其它服务调用了。
[root@172 ~]# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf [root@172 ~]# ldconfig [root@172 ~]# ldconfig -v |grep mysql ldconfig: 无法对 /libx32 进行 stat 操作: 没有那个文件或目录 ldconfig: 多次给出路径“/usr/lib” ldconfig: 多次给出路径“/usr/lib64” ldconfig: 无法对 /usr/libx32 进行 stat 操作: 没有那个文件或目录 /usr/local/mysql/lib: libmysqlclient.so.20 -> libmysqlclient.so.20.3.3
创建其它MySQL数据库用户
使用MySQL数据库root管理员用户登录MySQL数据库后,可以管理数据库和其他用户了。这里演示创建一个名为evai的MySQL用户(密码为@Typeusers2016.com)和一个名为typeusers的数据库。
[root@172 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 16 Server version: 5.7.16-log Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
######登录成功后,创建typecodes数据库,并设置字符集
mysql> CREATE DATABASE `typeusers` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; Query OK, 1 row affected (0.01 sec)
######创建名为typecodes用户,并让它拥有typecodes数据库所有的权限
mysql> grant all privileges on typeusers.* to evai@localhost identified by '@Typeusers2016.com'; Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> exit
Bye
ok,退出,然后用刚创建的用户登录到mysql:
[root@172 ~]# mysql -uevai -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 17 Server version: 5.7.16-log Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql>
安装nginx
去官网 http://nginx.org 下载最新的稳定版本,复制链接:
下载并解压:
[root@172 ~]# wget http://nginx.org/download/nginx-1.10.2.tar.gz --2016-10-23 13:22:15-- http://nginx.org/download/nginx-1.10.2.tar.gz 正在解析主机 nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 95.211.80.227 正在连接 nginx.org (nginx.org)|206.251.255.63|:80... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:910812 (889K) [application/octet-stream] 正在保存至: “nginx-1.10.2.tar.gz” 100%[=================================================>] 910,812 43.0KB/s 用时 18s 2016-10-23 13:22:35 (49.4 KB/s) - 已保存 “nginx-1.10.2.tar.gz” [910812/910812])
[root@172 ~]# tar -zxvf nginx-1.10.2.tar.gz #解压
在下载一个叫pcre的东东,PCRE 简介:PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是很有用的。Boost太庞大了,使用boost regex后,程序的编译速度明显变慢。测试了一下,同样一个程序,使用boost::regex编译时需要3秒,而使用pcre不到1秒。因此改用pcre来解决C语言中使用正则表达式的问题,简单来说可以进行URL重写,我们下载过来并解压
[root@172 ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
[root@172 ~]# tar -zxvf pcre-8.39.tar.gz
进入到nginx文件夹目录下,编译安装:
./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.39 #回车
make && make install
安装完成后进入 /usr/local/nginx 目录下,可以看到已经安装完毕:
[root@172 nginx-1.10.2]# cd /usr/local/nginx/ [root@172 nginx]# ll 总用量 4 drwxr-xr-x. 2 root root 4096 10月 23 13:43 conf drwxr-xr-x. 2 root root 38 10月 23 13:43 html drwxr-xr-x. 2 root root 6 10月 23 13:43 logs drwxr-xr-x. 2 root root 18 10月 23 13:43 sbin
接下来启动nginx,看到nginx已经启动:
[root@172 nginx]# cd sbin/ [root@172 sbin]# ./nginx [root@172 sbin]# ps -aux |grep nginx root 32128 0.0 0.0 18112 592 ? Ss 13:46 0:00 nginx: master process ./nginx nobody 32129 0.0 0.1 18532 1308 ? S 13:46 0:00 nginx: worker process root 32131 0.0 0.0 112664 984 pts/0 R+ 13:46 0:00 grep --color=auto nginx [root@172 sbin]#
我们用curl http:/127.0.0.1 测试下是否已经可以看到信息:
[root@172 nginx]# curl http://127.0.0.1 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
没有问题,打开浏览器输入ip地址访问:
也是成功的,不过有些小伙伴会打不开,原因是防火墙开启了导致访问失败,CentOS 7默认使用的是firewall作为防火墙,我们输入 systemctl stop firewalld.service 把它关闭,再次访问浏览器就可以看到了。附上防火墙的命令:
systemctl start firewalld.service #启动firewall systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动
这个时候我们还不能访问php文件的网页的,因为nginx还无法解析它,这个时候该php-fpm大展身手了:
[root@172 nginx]# cd /usr/local/php7/sbin/ [root@172 sbin]# ll 总用量 28168 -rwxr-xr-x. 1 root root 28839999 10月 22 23:43 php-fpm [root@172 sbin]# ./php-fpm [23-Oct-2016 14:14:51] ERROR: failed to open configuration file '/usr/local/php7/etc/php-fpm.conf': No such file or directory (2) [23-Oct-2016 14:14:51] ERROR: failed to load configuration file '/usr/local/php7/etc/php-fpm.conf' [23-Oct-2016 14:14:51] ERROR: FPM initialization failed
提示找不到php-fpm.conf文件,进入到 /usr/local/php7/etc 查看,发现只有default文件,把它copy一份到当前目录下命名为php-fpm.conf
[root@172 sbin]# cd /usr/local/php7/etc [root@172 etc]# ll 总用量 12 -rw-r--r--. 1 root root 1239 10月 22 23:43 pear.conf -rw-r--r--. 1 root root 4465 10月 22 23:43 php-fpm.conf.default drwxr-xr-x. 2 root root 29 10月 22 23:43 php-fpm.d
[root@172 etc]# cp php-fpm.conf.default php-fpm.conf
再次启动还是报错,发现conf.d目录下没有.conf后缀的文件,一样copy一份到该目录下改为www.conf ,再启动就成功了:
[root@172 etc]# /usr/local/php7/sbin/php-fpm [23-Oct-2016 14:19:14] WARNING: Nothing matches the include pattern '/usr/local/php7/etc/php-fpm.d/*.conf' from /usr/local/php7/etc/php-fpm.conf at line 125. [23-Oct-2016 14:19:14] ERROR: No pool defined. at least one pool section must be specified in config file [23-Oct-2016 14:19:14] ERROR: failed to post process the configuration [23-Oct-2016 14:19:14] ERROR: FPM initialization failed [root@172 etc]# cd php-fpm.d [root@172 php-fpm.d]# ll 总用量 20 -rw-r--r--. 1 root root 18521 10月 22 23:43 www.conf.default [root@172 php-fpm.d]# cp www.conf.default www.conf [root@172 php-fpm.d]# /usr/local/php7/sbin/php-fpm [root@172 php-fpm.d]# ps -aux | grep php-fpm root 32369 0.0 0.2 148336 2548 ? Ss 14:22 0:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf) nobody 32370 0.0 0.2 148336 2200 ? S 14:22 0:00 php-fpm: pool www nobody 32371 0.0 0.2 148336 2200 ? S 14:22 0:00 php-fpm: pool www root 32373 0.0 0.0 112664 984 pts/1 R+ 14:23 0:00 grep --color=auto php-fp [root@172 php-fpm.d]#
然后打开编辑 /usr/local/nginx/conf/nginx.conf 文件,找到下面这段并改为如下图:
location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
再进入html目录下,创建一个test.php文件:
[root@172 nginx]# cd html/ [root@172 html]# ll 总用量 8 -rw-r--r--. 1 root root 537 10月 23 13:43 50x.html -rw-r--r--. 1 root root 612 10月 23 13:43 index.html [root@172 html]# vim test.php #输入 <?php phpinfo(); #保存退出 [root@172 html]# ../sbin/nginx -s reload #重新载入nginx
打开浏览器,输入你的ip地址:http://xxx.xxx.xx.xxx/test.php 访问:
至此,lnmp环境搭建完成。