一、什么是LNMP架构
L:基于linux系统的架构
N:使用nginx软件
M:使用MySQL数据库
p:使用PHP,python等语音
的综合web架构
二、安装nginx软件
-
上传nginx源码包(可以从nginx.org下载),并归档到/usr/src/目录下
通过lrzsz上传[root@localhost ~]# tar xf nginx-1.16.0.tar.gz -C /usr/src/ [root@localhost ~]# ls /usr/src/ debug kernels nginx-1.16.0
-
安装源码编译是需要的函数库,和编译软件
pcre-devel:正则支持包
zlib-devel:解压支持的包
openssl-devel:支持加密[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel openssl-devel
-
创建运行用户,组
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
-
编译安装nginx
root@localhost ~]# cd /usr/src/nginx-1.16.0/ [root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module&&make && make install
--prefix 设定Nginx的安装目录 --user和--group 指定Nginx运行用户和组 --with-http_stub_status_module 启用http_stub_status_module模块以支持状态统计 --with-http_ssl_module 启用SSL模块 --with-http_flv_module 启用FLV模块,提供寻求内存使用基于时间的偏移量文件 --with-http_gzip_static_module 此模块的作用就是在接到请求后,会到url相同的路径的文件系统去找扩展名为“.gz”的文件
三、安装mysql数据库
-
上传mysql源码包和boost源码包解压
mysql需要boost的支持[root@localhost ~]# mkdir /usr/local/boost [root@localhost ~]# ls anaconda-ks.cfg boost_1_59_0.tar.gz mysql-5.7.24.tar.gz nginx-1.16.0.tar.gz [root@localhost ~]# tar xf boost_1_59_0.tar.gz -C /usr/local/boost/ [root@localhost ~]# tar xf mysql-5.7.24.tar.gz -C /usr/src/
-
安装编译是需要的函数库或编译工具
[root@localhost ~]# yum -y install ncurses-devel cmake
-
创建运行的用户,组
[root@localhost ~]# useradd -M -s /sbin/nologin mysql
-
编译安装mysql
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc -DWITH_BOOST=/usr/local/boost&& make && make install
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql //数据库程序安装目录
-DDEFAULT_CHARSET=utf8 //指定字符集编码
-DDEFAULT_COLLATION=utf8_general_ci //默认的字符集校对规则,utf8_general_ci适用于utf-8字符集的通用规则
-DWITH_EXTRA_CHARSETS=all //指定额外支持的字符集编码
-DSYSCONFDIR=/etc //指定配置文件存放目录
-
设置mysql中文件属主属组
[root@localhost mysql]# chown -R mysql:mysql ./ #递归设置将所有文件目录
-
修改配置文件
[root@localhost ~]# vim /etc/my.cnf [root@localhost ~]# cat /etc/my.cnf [mysqld] datadir=/usr/local/mysql/data #指定data目录位置 socket=/tmp/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/usr/local/mysql/data/mysql.log #指定错误日志位置 pid-file=/usr/local/mysql/data/mysql.pid #指定pid文件位置 # # include all files from the config directory # !includedir /etc/my.cnf.d
-
初始化mysql数据库
[root@localhost ~]# /usr/local/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize 2019-09-10T21:45:51.102478Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-09-10T21:45:54.725086Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-09-10T21:45:54.790635Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-09-10T21:45:54.846924Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5dbacf71-d414-11e9-924b-000c29def907. 2019-09-10T21:45:54.848308Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-09-10T21:45:54.849392Z 1 [Note] A temporary password is generated for root@localhost: <dks1t4/1Jky #root@localhost:后面的密码一定记住,后边要用来登录数据库
--basedir=/usr/local/mysql/ //指定安装目录(产品目录)
--datadir=/usr/local/mysql/data //指定数据目录
--user=mysql //指定用户身份
-
添加mysql命令到变量中
[root@localhost ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile [root@localhost ~]# source /etc/profile #执行脚本是变量生效
-
设置启动文件,启动mysql服务
#将mysql的启动文件复制到/etc/init.d/下改名为mysqld [root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [root@localhost ~]# /etc/init.d/mysqld start Starting MySQL.Logging to '/usr/local/mysql/data/mysql.log'. . SUCCESS!
-
查看是否启动
[root@localhost ~]# netstat -lunpt | grep mysql tcp6 0 0 :::3306 :::* LISTEN 28282/mysqld
-
修改mysql数据库密码
[root@localhost ~]# mysql -u root -p'<dks1t4/1Jky' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 18 Server version: 5.7.24 Source distribution Copyright (c) 2000, 2018, 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> mysql> set password = password('123456'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> quit; #重新登录,成功登录修改密码成功 [root@localhost ~]# mysql -u root -p'123456' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 19 Server version: 5.7.24 Source distribution Copyright (c) 2000, 2018, 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>
三、安装PHP软件
-
上传PHP安装包,解归档
[root@localhost ~]# tar xf php-5.6.39.tar.gz -C /usr/src/
-
安装编译所需要的函数库和编译工具
[root@localhost ~]# yum -y install gd libxml2-devel libjpeg-devel libpng-devel
-
创建程序用户和组
[root@localhost ~]# useradd -M -s /sbin/nologin php
-
编译安装PHP
[root@localhost ~]# cd /usr/src/php-5.6.39/ [root@localhost php-5.6.39]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib && make && make install
-
生成主配置文件,加载PHP命令使系统可用
#将源码包下的 php.ini-production 复制到 /usr/local/php5/下改名为php.ini [root@localhost php-5.6.39]# cp php.ini-production /usr/local/php5/php.ini [root@localhost php-5.6.39]# ln -s /usr/local/php5/bin/ /usr/local/bin/ [root@localhost php-5.6.39]# ln -s /usr/local/php5/sbin/ /usr/local/sbin/
-
安装ZendGuardLander(注意:若是64位系统,该软件得到其官网下载64位的相应软件包,若用32位的就会报错。下载地址:http://www.zend.com/en/products/guard/downloads
[root@localhost ~]# tar xf zend-loader-php5.6-linux-x86_64_update1.tar.gz [root@localhost ~]# ls anaconda-ks.cfg mysql-5.7.24.tar.gz php-5.6.39.tar.gz zend-loader-php5.6-linux-x86_64_update1.tar.gz boost_1_59_0.tar.gz nginx-1.16.0.tar.gz zend-loader-php5.6-linux-x86_64 [root@localhost ~]# cd zend-loader-php5.6-linux-x86_64 [root@localhost zend-loader-php5.6-linux-x86_64]# ls opcache.so README.txt ZendGuardLoader.so [root@localhost zend-loader-php5.6-linux-x86_64]# cp ZendGuardLoader.so /usr/local/php5/lib/php/ #修改主配置文件开启 [root@localhost ~]# vim /usr/local/php5/php.ini zend_extension=/usr/local/php5/lib/php/vim zend_loader.enable=1
四、配置nginx支持PHP
[root@localhost ~]# cd /usr/local/php5/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# vim php-fpm.conf
25 pid = run/php-fpm.pid //确认pid文件位置
149 user = php //运行用户
150 group = php //运行组
246pm.start_servers = 20 //启动时开启的进程数
251pm.min_spare_servers = 5 //最少空闲进程数
256pm.max_spare_servers = 35 //最大空闲进程数
241pm.max_children = 50 //最多空闲进程数
[root@localhost etc]# /usr/local/php5/sbin/php-fpm #启动fpm
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location ~\.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload #刷新配置
[root@localhost ~]# /usr/local/nginx/sbin/nginx #重启服务
五、测试
-
关闭防火墙,安全机制
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# iptables -F [root@localhost ~]# setenforce 0
-
书写测试文件
#测试PHP是否成功 [root@localhost ~]# vim /usr/local/nginx/html/php.php <?php phpinfo(); ?> #测试数据库是否可用 [root@localhost ~]# vim /usr/local/nginx/html/mysql.php <?php $link=mysqli_connect('localhost','root','123456'); //连接mysql数据库 if($link) echo "<h1>恭喜你,大功告成!!</h1>"; //连接成功则返回信息 mysqli_close(); //关闭数据库连接 ?> ~
-
测试结果