LNMP架设动态网站
Posted wang-hongwei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP架设动态网站相关的知识,希望对你有一定的参考价值。
1.安装nginx
1)使用Nginx官方的yum源
[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
2)安装Nginx
[root@localhost ~]# yum install nginx -y [root@localhost ~]# systemctl start nginx [root@localhost ~]# systemctl enable nginx
2.使用第三方源安装php7.2
1)移除旧版php
[root@localhost ~]# yum list installed | grep php [root@localhost ~]# yum remove php*
2)安装php源
[root@localhost ~]# yum install epel-release -y [root@localhost ~]# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
3)安装php-fpm
[root@localhost ~]# yum install php72w-fpm -y
4)安装php的扩展
[root@localhost ~]# yum -y install php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-pear php72w-devel
其他的扩展如下,按照自己的需求安装
php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml php72w php72w-bcmath php72w-dba php72w-enchant php72w-imap php72w-interbase php72w-intl php72w-ldap php72w-mcrypt php72w-odbc php72w-pdo_dblib php72w-pear php72w-pecl-apcu php72w-pecl-imagick php72w-pecl-xdebug php72w-pgsql php72w-phpdbg php72w-process php72w-pspell php72w-recode php72w-snmp php72w-soap php72w-tidy php72w-xmlrpc php72w-pecl-igbinary php72w-intl php72w-memcached php72w-pecl-mongodb
5)查看是否成功安装
[root@localhost ~]# php -v #查看是否成功安装 [root@localhost ~]# php -m #查看所有的扩展
6)配置文件所在的位置
php.ini /etc/php.ini phpize /usr/bin/phpize php-config /usr/bin/php-config php-fpm.conf /etc/php-fpm.conf php-fpm.pid /var/run/php-fpm/php-fpm.pid
7)启动php-fpm
[root@localhost ~]# systemctl start php-fpm [root@localhost ~]# systemctl enable php-fpm
3.安装MariaDB
1)使用MariaDB官方的yum源
[root@localhost ~]# vim /etc/yum.repos.d/mariadb.repo [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.4.0/centos74-aarch64/ #这个链接换成你要的版本地址 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
2)安装并启动MariaDB
[root@localhost ~]# yum install mariadb-server -y [root@localhost ~]# systemctl start mariadb [root@localhost ~]# systemctl enable mariadb
4.配置LNMP架构
1)配置Nginx实现动态请求转发至php
[root@localhost ~]# vim /etc/nginx/conf.d/php.conf server { listen 80; server_name default_server; root /usr/share/nginx/html; index index.php index.html; location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /soft/code$fastcgi_script_name; include fastcgi_params; } }
2)添加php测试页面
测试phpinfo
[root@nginx ~]# cat /usr/share/nginx/html/info.php <?php phpinfo(); ?>
使用mysqli模块测试连接mysql
[root@nginx ~]# cat /usr/share/nginx/html/mysqli.php <?php $servername = "localhost"; $username = "root"; $password = ""; // 创建连接 $conn = mysqli_connect($servername, $username, $password); // 检测连接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "连接成功"; ?>
使用pdo模块测试连接mysql
[root@nginx ~]# cat /usr/share/nginx/html/mysqlpdo.php <?php $servername = "localhost"; $username = "root"; $password = ""; try { $conn = new PDO("mysql:host=$servername;dbname=test", $username, $password); echo "连接成功"; } catch(PDOException $e) { echo $e->getMessage(); } ?>
5.PHP配置文件优化
1)php-ini优化
//打开php的安全模式,控制php执行危险函数, 默认是Off,改为On sql.safe_mode = Off //关闭php头部信息, 隐藏版本号, 默认是On,该为Off expose_php = On //错误信息输出控制 display_error = Off error_reporting = E_WARNING & E_ERROR //记录错误日志至后台, 方便追溯 log_errors = On error_log = /var/log/php_error.log //每个脚本时间最大内存 memory_limit = 128M //上传文件最大许可,默认2M, 建议调整为16,32M upload_max_filesize = 2M //禁止远程执行phpshell,默认On, 建议Off allow_url_fopen = On //时区调整,默认PRC, 建议调整为Asia/Shanghai date.timezone = PRC //整体优化后配置文件 sql.safe_mode = Off expose_php = Off display_error = Off error_reporting = E_WARNING & E_ERROR log_errors = On error_log = /var/log/php_error.log upload_max_filesize = 50M allow_url_fopen = Off date.timezone = Asia/Shanghai
2)php-fpm优化
PHP-FPM配置文件 4核16G、8核16G
[root@localhost ~]# cat /etc/php-fpm.d/www.conf [global] pid = /var/run/php-fpm.pid #php-fpm程序错误日志 error_log = /var/log/php/php-fpm.log log_level = warning rlimit_files = 655350 events.mechanism = epoll [www] user = nginx group = nginx listen = 127.0.0.1:9000 listen.owner = www listen.group = www listen.mode = 0660 listen.allowed_clients = 127.0.0.1 pm = dynamic pm.max_children = 512 pm.start_servers = 10 pm.min_spare_servers = 10 pm.max_spare_servers = 30 pm.process_idle_timeout = 15s; pm.max_requests = 2048 #php-www模块错误日志 php_flag[display_errors] = off php_admin_value[error_log] = /var/log/php/php-www.log php_admin_flag[log_errors] = on #php慢查询日志 request_slowlog_timeout = 5s slowlog = /var/log/php/php-slow.log PHP5-FPM配置详解释 [global] #pid设置, 记录程序启动后pid pid = /var/run/php-fpm.pid #php-fpm程序启动错误日志路径 error_log = /soft/log/php/php-fpm_error.log # 错误级别. 可用级别为: alert(必须立即处理),error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice. log_level = warning #设置文件打开描述符的rlimit限制. rlimit_files = 65535 events.mechanism = epoll #启动进程的用户和组 [www] user = www group = www # fpm监听端口 listen = 127.0.0.1:9000 # unix socket设置选项,如果使用tcp方式访问,这里注释即可。 listen.owner = www listen.group = www # 允许访问FastCGI进程的IP,any不限制 listen.allowed_clients = 127.0.0.1 # pm设置动态调度 pm = dynamic # 同一时刻最大的php-fpm子进程数量 pm.max_children = 200 # 动态方式下的起始php-fpm进程数量 pm.start_servers = 20 # 动态方式下服务器空闲时最小php-fpm进程数量 pm.min_spare_servers = 10 # 动态方式下服务器空闲时最大php-fpm进程数量 pm.max_spare_servers = 30 # 最大请求 pm.max_requests = 1024 pm.process_idle_timeout = 15s; # FPM状态页面,用于监控php-fpm状态使用 pm.status_path = /status # 错误日志 php_flag[display_errors] = off php_admin_value[error_log] = /soft/log/php/php-www_error.log php_admin_flag[log_errors] = on # 配置php慢查询, 以及慢查询记录日志位置 request_slowlog_timeout = 5s slowlog = /soft/log/php/php-slow.log
以上是关于LNMP架设动态网站的主要内容,如果未能解决你的问题,请参考以下文章