本篇文章介绍LNMP架构部署项目,包括Nginx安装配置、PHP安装配置、数据库Mariadb安装配置、Nginx连通PHP、PHP连通MySQL、拆分数据库至独立服务器、拆分静态资源至服务器、扩展一个web服务器。
LNMP架构
Linux Nginx Mysql PHP
Nginx不能处理动态请求,如果是动态请求,就通过fastcgi协议转交给后端的PHP程序处理
Nginx安装配置
第一步:安装nginx
配置官网yum仓库
1. vim /etc/yum.repos.d/nginx.repo 2. 3. [nginx-stable] 4. name=nginx stable repo 5. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ 6. gpgcheck=1 7. enabled=1 8. gpgkey=https://nginx.org/keys/nginx_signing.key 9. module_hotfixes=true
第二步:创建虚拟用户
1. [root@Web01 ~]# groupadd -g 666 www 2. [root@Web01 ~]# useradd -u666 -g666 -M -s /sbin/nologin www
第三步:配置Nginx启动用户为www
1. [root@Web01 ~]# vim /etc/nginx/nginx.conf 2. 3. user www; 4. worker_processes auto;
第四步:启动Nginx加入开机自启动
1. [root@Web01 ~]# systemctl start nginx 2. [root@Web01 ~]# systemctl enable nginx 3. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
PHP安装配置
第一步:配置PHP软件仓库并安装PHP
1. #配置PHP仓库 2. [root@Web01 ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 3. [root@Web01 ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm 4. 5. [root@Web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common 6. 7. #配置第三方源 8. [root@Web01 ~]# vim /etc/yum.repos.d/php.repo 9. [php-webtatic] 10. name = PHP Repository 11. baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/ 12. gpgcheck = 0 13. 14. #安装PHP 15. [root@Web01 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb --nogpgcheck
或者是使用rpm包,上传后解压。
yum -y localinstall *.rpm
第二步:修改PHP启动用户为www
1. [root@Web01 ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf 2. [root@Web01 ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
第三步:启动服务并设置开机自启
1. [root@Web01 ~]# systemctl start php-fpm 2. [root@Web01 ~]# systemctl enable php-fpm 3. Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
第四步:查看状态
1. [root@Web01 ~]# netstat -tnulp 2. Active Internet connections (only servers) 3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 4. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1272/sshd 5. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1413/master 6. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4133/php-fpm: maste 7. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3989/nginx: master 8. tcp6 0 0 :::22 :::* LISTEN 1272/sshd 9. tcp6 0 0 ::1:25 :::* LISTEN 1413/master 10. udp 0 0 127.0.0.1:323 0.0.0.0:* 840/chronyd 11. udp6 0 0 ::1:323 :::* 840/chronyd
安装数据库mariadb-server
第一步:安装数据库
[root@Web01 ~]# yum install mariadb-server -y
第二步:启动数据库、设置开机自启动、查看状态
1. [root@Web01 ~]# systemctl start mariadb 2. [root@Web01 ~]# systemctl enable mariadb 3. Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. 4. [root@Web01 ~]# netstat -tnulp 5. Active Internet connections (only servers) 6. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 7. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1272/sshd 8. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1413/master 9. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4133/php-fpm: maste 10. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4480/mysqld 11. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3989/nginx: master 12. tcp6 0 0 :::22 :::* LISTEN 1272/sshd 13. tcp6 0 0 ::1:25 :::* LISTEN 1413/master 14. udp 0 0 127.0.0.1:323 0.0.0.0:* 840/chronyd 15. udp6 0 0 ::1:323 :::* 840/chronyd
第三步:配置用户密码,默认是root
1. [root@Web01 ~]# mysqladmin password '666666' #设置密码为666666 2. [root@Web01 ~]# mysql -uroot -p666666 #登录数据库 3. Welcome to the MariaDB monitor. Commands end with ; or \g. 4. Your MariaDB connection id is 3 5. Server version: 5.5.68-MariaDB MariaDB Server 6. 7. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. 8. 9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 10. 11. MariaDB [(none)]> show databases; #查看有哪些库 12. +--------------------+ 13. | Database | 14. +--------------------+ 15. | information_schema | 16. | mysql | 17. | performance_schema | 18. | test | 19. +--------------------+ 20. 4 rows in set (0.00 sec) 21. 22. MariaDB [(none)]> create database hehe; #创建hehe库 23. Query OK, 1 row affected (0.00 sec) 24. 25. MariaDB [(none)]> drop database hehe; #删除hehe库 26. Query OK, 0 rows affected (0.01 sec) 27. 28. MariaDB [(none)]> quit #退出数据库 29. Bye 30. [root@Web01 ~]#
注意:配置数据库密码需要服务启动后才能配置
Nginx连通PHP
1. [root@Web01 /]# cat /etc/nginx/conf.d/default.conf 2. server { 3. listen 80; 4. server_name php.koten.com; 5. 6. location / { 7. root /code; 8. index index.php index.html; 9. } 10. 11. location ~ \.php$ { 12. root /code; 13. fastcgi_pass 127.0.0.1:9000; 14. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 15. include fastcgi_params; 16. } 17. } 18. [root@Web01 /]# systemctl restart nginx 19. [root@Web01 /]# vim /code/index.php 20. <?php 21. phpinfo(); 22. ?>
windows本地hosts解析后,即可访问PHP有关信息
PHP连通MySQL
1. [root@Web01 /]# vim /code/mysql.php 2. <?php 3. $servername = "localhost"; 4. $username = "root"; 5. $password = "123"; 6. 7. // 创建连接 8. $conn = mysqli_connect($servername, $username, $password); 9. 10. // 检测连接 11. if (!$conn) { 12. die("Connection failed: " . mysqli_connect_error()); 13. } 14. echo "php可以连接MySQL..."; 15. ?>