LNMP
原创
©著作权归作者所有:来自51CTO博客作者wx63f4605f9313d的原创作品,请联系作者获取转载授权,否则将追究法律责任
LNMP
- nginx fastCGI PHP-FPM PHP
//安装依赖:
[root@cong11 ~]# yum -y install gcc autoconf freetype gd libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel freetype-devel libjpeg-devel bzip2 bzip2-devel openssl openssl-devel
//安装libmcrypt
上传软件包
[root@cong11 ~]# ls
anaconda-ks.cfg libmcrypt-2.5.7.tar.gz nginx-1.10.3.tar.gz php-5.6.36.tar.gz
[root@cong11 ~]# tar zxf libmcrypt-2.5.7.tar.gz
[root@cong11 ~]# cd libmcrypt-2.5.7/
[root@cong11 libmcrypt-2.5.7]# ./configure --prefix=/usr/local/libmcrypt && make && make install
//解压PHP包
[root@cong11 ~]# tar -zxvf php-5.6.36.tar.gz -C /usr/local/src/
//安装php
//预编译
[root@cong11 ~]# cd /usr/local/src/php-5.6.36/
[root@cong11 php-5.6.36]# ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/usr/local/php5.6/etc/ --with-bz2 --enable-maintainer-zts
//编译
[root@cong11 php-5.6.36]# make
//安装
[root@cong11 php-5.6.36]# make install
[root@localhost php-5.6.36]# pwd
/usr/local/src/php-5.6.36
[root@localhost php-5.6.36]# cp /usr/local/src/php-5.6.36/php.ini-production /usr/local/php5.6/etc/php.ini
[root@localhost php-5.6.36]# ls /usr/local/php5.6/etc/
pear.conf php-fpm.conf.default php.ini
[root@localhost php-5.6.36]# cd /usr/local/php5.6/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@cong11 etc]# vim php-fpm.conf
user = www
group = www
pid = run/php-fpm.pid
listen = 0.0.0.0:9000
pm.max_children =300
pm.start_servers =20
pm.min_spare_servers = 20
pm.max_spare_servers = 100
//复制php-fpm启动脚本到init.d
[root@cong11 ~]# cp /usr/local/src/php-5.6.36/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
//赋予执行权限
[root@cong11 ~]# chmod +x /etc/init.d/php-fpm
//添加开机启动
[root@cong11 ~]# chkconfig --add php-fpm
[root@cong11 ~]# chkconfig php-fpm on
[root@localhost ~]# chkconfig --list php-fpm
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
//启动服务
[root@cong11 ~]# /etc/init.d/php-fpm start
Starting php-fpm done
//查看端口监听状态
[root@cong11 ~]# netstat -antpu | grep php-fpm
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 7767/php-fpm: maste //PHP的环境已经配置好了
- 配置nginx 用fastCGI协议 调用PHP-FPM块 调用PHP
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
user www www;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;
server_tokens off;
client_max_body_size 10m;
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /usr/local/nginx/nginx_tmp;
fastcgi_intercept_errors on;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=ngx_fcgi_cache:128m inactive=1d max_size=10g;
gzip on;
gzip_min_length 2k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
gzip_proxied any;
server {
listen 80;
server_name www.benet.com;
charset utf-8;
access_log logs/host.access.log main;
location ~* ^.+\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
valid_referers none blocked www.benet.com benet.com;
if ($invalid_referer) {
#return 302 http://www.benet.com/img/nolink.jpg;
return 404;
break;
}
expires 365d;
access_log off;
}
location / {
root html;
index index.php index.html index.htm;
}
location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ {
expires 30d;
#log_not_found off;
access_log off;
}
location ~* \.(js|css)$ {
expires 7d;
log_not_found off;
access_log off;
}
location = /(favicon.ico|roboots.txt) {
access_log off;
log_not_found off;
}
location /status {
stub_status on;
}
location ~ .*\.(php|php5)?$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache ngx_fcgi_cache;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key http://$host$request_uri;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
[root@localhost ~]# vim /usr/local/nginx/html/index.php
//测试访问192.168.70.106/index.php
[root@localhost ~]# yum list | grep mariadb
mariadb-libs.x86_64 1:5.5.68-1.el7 @anaconda
mariadb.x86_64 1:5.5.68-1.el7 base
mariadb-bench.x86_64 1:5.5.68-1.el7 base
mariadb-devel.i686 1:5.5.68-1.el7 base
mariadb-devel.x86_64 1:5.5.68-1.el7 base
mariadb-embedded.i686 1:5.5.68-1.el7 base
mariadb-embedded.x86_64 1:5.5.68-1.el7 base
mariadb-embedded-devel.i686 1:5.5.68-1.el7 base
mariadb-embedded-devel.x86_64 1:5.5.68-1.el7 base
mariadb-libs.i686 1:5.5.68-1.el7 base
mariadb-server.x86_64 1:5.5.68-1.el7 base
mariadb-test.x86_64 1:5.5.68-1.el7 base
[root@localhost ~]# yum -y install mariadb mariadb-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]# netstat -anplt | grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1701/mysqld
//创建连接数据库的账号,按文档做。默认账号密码为空
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on *.* to test@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit
Bye
//编写一个连接数据库的测试代码
[root@localhost ~]# vim /usr/local/nginx/html/test.php
//测试192.168.70.106/test.php
[root@localhost ~]# curl 192.168.70.106/test.php
connection success......[root@localhost ~]#
//查错
cat /usr/local/nginx/logs/access.log
cat /usr/local/nginx/logs/error.log
mysql -utest -p //确认能连接数据库
[root@localhost ~]# ls /var/lib/mysql/mysql.sock //数据库自动生成文件
/var/lib/mysql/mysql.sock
[root@localhost ~]# ls /tmp/ //没有生成mysql.sock文件
pear
systemd-private-82ff4b483d544a3d81eca672444387c2-mariadb.service-5dwDo6
vmware-root_648-2688619569
vmware-root_649-4021719023
vmware-root_650-2696943027
vmware-root_651-4013395565
vmware-root_652-2697074100
vmware-root_655-4021587944
vmware-root_656-2689274927
vmware-root_657-4022112241
vmware-root_658-2697598381
vmware-root_659-4013788787
yum_save_tx.2023-05-10.14-41.uHnH_a.yumtx
//解决方法,设置符号连接
[root@localhost ~]# ln -s /var/lib/mysql/mysql.sock /tmp/
[root@localhost ~]# ls /tmp/
mysql.sock
pear
systemd-private-82ff4b483d544a3d81eca672444387c2-mariadb.service-5dwDo6
vmware-root_648-2688619569
vmware-root_649-4021719023
vmware-root_650-2696943027
vmware-root_651-4013395565
vmware-root_652-2697074100
vmware-root_655-4021587944
vmware-root_656-2689274927
vmware-root_657-4022112241
vmware-root_658-2697598381
vmware-root_659-4013788787
yum_save_tx.2023-05-10.14-41.uHnH_a.yumtx
//强制覆盖符号链接
[root@localhost ~]# ln -s /var/lib/mysql/mysql.sock /tmp/
ln: failed to create symbolic link ‘/tmp/mysql.sock’: File exists
[root@localhost ~]# ln -sf /var/lib/mysql/mysql.sock /tmp/
//删除自建发fastCGI缓存
[root@localhost ~]# curl localhost/test.php
<?php
$link=mysql_connect('localhost','test','123456');
if ($link)echo "connection success......";
mysql_close();
?>