linux nginx php74 mysql 建站全过程


linux下检查是否安装过某软件包
1、rpm包安装的,可以用 rpm -qa 看到,如果要查找某软件包是否安装,用 rpm -qa | grep "软件或者包的名字"
2、以deb包安装的,可以用 dpkg -l 看到。如果是查找指定软件包,用 dpkg -l | grep "软件或者包的名字"
3、yum方法安装的,可以用 yum list installed 查找,如果是查找指定包,用yum list installed | grep "软件名或者包名"
4、查看有关于"软件名字"的所有安装包,用yum list | grep "软件名字"

常用Yum命令(yum查询只是查服务器上的)
yum list: 查询所有可用软件包列表
yum search 关键字: 搜索服务器上所有和关键字相关的包
yum -y install 包名(yum安装只需包名): 安装
-install:安装
-y:自动输入yes
yum -y update 包名: 升级
yum -y remove 包名: 卸载(不会检查依赖,不建议使用)

yum软件组管理命令
yum grouplist: 列出所有可用的软件组列表
yum groupinstall 软件组名: 安装指定软件组
yum groupremove 软件组名: 卸载指定软件组
echo $LANG: 查询当前系统语言
locale -a: 查看已安装的语言包
locale -a | grep en: 查看英文语言
修改系统语言:
临时修改: LANG=语言名称
永久修改: vim /etc/locale.conf(改完重启)


一、准备

服务器一台,镜像我这里用的Aliyun Linux 2
Aliyun Linux 2默认使用的是阿里的yum源
更新yum

yum -y update

二、安装Nginx

1、Nginx资料

Nginx 的中文文档https://www.nginx.cn/doc/
Nginx 的下载页面http://nginx.org/en/download.html

2、安装依赖

安装epel-release

yum install -y epel-release

gccgcc-c++fast-cgiwgetyum 这些是基础就不说了。
安装nginx时需要的依赖:zlibzlib-developensslopenssl-develprceprce-devel
pcre: 在使用 nginx 的 rewrite 模块的时候,需要有pcre库的支持
openssl: 在使用ssl功能时,需要有 openssl库的支持
zlib:  在使用gzip模块时,需要有zlib库的支持。

安装依赖:

yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
//如果之前没有安装过gcc等工具,使用下面的无脑安装
//yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers zlib zlib-devel openssl openssl-devel pcre pcre-devel

3、安装 nginx

yum -y install nginx

查看nginx的安装程序都安装在哪里了

rpm -ql nginx

4、启动Nginx

启动nginx并设置开机自动运行

systemctl start nginx    #启动,restart-重启,stop-停止
systemctl enable nginx    #开机启动

这个时候输入自己的域名就可以访问了!


三、安装PHP

1、PHP资料

PHP官网 https://www.php.net/
php7相比较于php5有较大差别,至于取舍看个人。我是个人使用选择了php7。

2、PHP依赖

Remi repository 是包含最新版本 PHP 和 MySQL 包的 Linux 源,由 Remi 提供维护。有个这个源之后,使用 YUM 安装或更新 PHP、MySQL、phpMyAdmin 等服务器相关程序的时候就非常方便了。
首先使用uname查看系统内核:-r 打印内核版本;-o打印操作系统。

[root@YJW ~]# uname -or
4.19.91-19.1.al7.x86_64 GNU/Linux

根据自己的内核版本添加第三方REMI源:

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm 

安装好REMI源后,可以使用yum search php74查看有哪些包。

3、PHP安装

安装PHP 7.4.X及相关模块

yum install -y php74-php-fpm php74-php-cli php74-php-bcmath php74-php-gd php74-php-json php74-php-mbstring php74-php-mcrypt php74-php-mysqlnd php74-php-opcache php74-php-pdo php74-php-pecl-crypto php74-php-pecl-mcrypt php74-php-pecl-geoip php74-php-recode php74-php-snmp php74-php-soap php74-php-xml php74-php-imagick php74-php-pecl-zip 

查看PHP版本

[root@YJW ~]# php74 -v
PHP 7.4.10 (cli) (built: Sep  1 2020 13:58:08) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.10, Copyright (c), by Zend Technologies

查看安装了哪些PHP的拓展

php74 -m

4、启动PHP

查看PHP是否开启ps -ef|grep php

[root@YJW ~]# ps -ef|grep php
root     24181 23675  0 17:35 pts/2    00:00:00 grep --color=auto php

看到上面是没有启动的,启动php并设置开机自动运行。

systemctl start php74-php-fpm
systemctl enable php74-php-fpm

四、环境搭建

1、前期准备

首先建立一个工作目录,我选择创建 /var/www/web 目录。

mkdir /var/www/web

在web目录下创建一个简单的index.html文件。

[root@YJW web]# cd /var/www/web/
[root@YJW web]# ls
index.html
[root@YJW web]# cat index.html
<html>
<head>
  <title>YJW</title>
</head>
<body>
  <h1>Nginx Test!</h1>
</body>
</html>

在web目录下创建一个PHP的信息显示页面info.php

[root@YJW web]# cat index.php
<?php
phpinfo();
?>

在web目录下创建一个index.php文件。

[root@YJW web]# cat index.php
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>杨佳伟</title>
</head>
<body>
  <h1>PHP Test!</h1>
  <?php 
  echo "Hello World!  PHP"; 
  ?> 
</body>
</html>

目录下文件如下:

[root@YJW web]# ls
index.html  index.php  info.php

创建用户和组,创建之前我们先用ps aux | grep nginx查看一下php的进程。我们可以看到php7默认新建了用户和组 nginx,就不需要再自己创建用户和组了。

[root@YJW ~]# ps aux | grep nginx
root       895  0.0  0.2 120928  2152 ?        Ss   19:54   0:00 nginx: master process /usr/sbin/nginx
nginx      896  0.0  0.6 121312  6616 ?        S    19:54   0:00 nginx: worker process
root      1239  0.0  0.2 112824  2296 pts/3    S+   20:41   0:00 grep --color=auto nginx

2、关联Nginx

更改nginx默认的网页目录,需要更改nginx.conf文件,可使用rpm -ql nginx查看文件所在路径。我的路径为/etc/nginx/nginx.conf
编辑nginx.conf文件:
1、网页的默认路径为/usr/share/nginx/html,更为之前创建的路径/var/www/web
2、location / { }中添加index等。

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        #root         /usr/share/nginx/html;
        root         /var/www/web;
        
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
			index index.html;
        }
    }

检查nginx语法

[root@YJW nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载nginx服务

nginx -s reload

在浏览器输入自己的ip地址进行查看就可以啦。
在这里插入图片描述

3、关联PHP

接下来我们关联PHP代码。首先使用netstat -ntlp查看一下端口的占用情况。nginx占用80端口,php-fpm占用9000端口。

[root@YJW ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      739/sshd
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      1511/php-fpm: maste
tcp        0      0 0.0.0.0:5355            0.0.0.0:*               LISTEN      508/systemd-resolve
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      441/rpcbind
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      895/nginx: master p
tcp6       0      0 :::5355                 :::*                    LISTEN      508/systemd-resolve
tcp6       0      0 :::111                  :::*                    LISTEN      441/rpcbind
tcp6       0      0 :::80                   :::*                    LISTEN      895/nginx: master p

nginx.conf文件中继续添加支持PHP的配置信息。添加信息如下:
其中第2行网站根目录更改为自己创建的网站目录,之前我创建的为/var/www/web

location ~ \.php$ {
        root           /var/www/web;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

重新加载nginx服务

nginx -s reload

打开浏览器输入ip/info.php可以查看php的信息页面。
在这里插入图片描述

3、文件显示

之前我们在/var/www/web文件夹下还建立了index.php文件,接下来去显示这个文件。在网址栏输入ip/index.php即可显示。
在这里插入图片描述
上图可以看到输入ip地址,默认显示的页面是index.html,如果想要存在index.phpindex.html时,默认打开index.php时,我们进行如下配置。
我们打开nginx.conf文件,location / { }index.html的前面添加index.php,顺便在最后添加index.htm。当都存在是按照顺序显示。

        location / {
			index index.php index.html index.htm;
        }

保存,nginx -t检查,nginx -s reload重载,之后输入ip地址查看。
在这里插入图片描述
对于nginx.conf里面的一些功能下方会有一些简单的介绍。





十、笔记

1、Nginx常用

nginx -t     : 测试配置文件是否有语法错误
nginx -s reopen: 重启Nginx
nginx -s reload: 重新加载Nginx配置文件,然后以优雅的方式重启Nginx
nginx -s stop : 强制停止Nginx服务
nginx -s quit : 优雅地停止Nginx服务(即处理完所有请求后再停止服务)

检查端口是否被占用

netstat -ntlp

检查nginx是否已经启动

ps -aux | grep nginx

查杀进程

pkill -9 nginx

2、PHP常用

PHP卸载
卸载php使用yum remove php是卸载不干净的,之后php -v还可以查看到信息。
所以卸载时,要先卸载没有依赖项的。先查找php相应文件:

rpm -qa|grep php

之后使用rpm -e指令按照 php-mysql php-pdo php-xml php-cli php-gd php-common的顺序卸载

3、nginx.conf文件介绍

完整的文件内容如下:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# 动态加载模块,查看/usr/share/doc/nginx/README.dynamic
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;


    # 从/etc/nginx/conf.d目录加载模块化配置文件。
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        #root         /usr/share/nginx/html;
        root         /var/www/web;
        
        # Load configuration files for the default server block.
        # 为默认服务器块加载配置文件。
        include /etc/nginx/default.d/*.conf;

        location / {
            index  index.php index.html index.htm;
        }

        location ~ \.php$ {
            root           /var/www/web;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }        

        #404错误页面    
        error_page 404 /404.html;
            location = /40x.html {
        }
        #50X错误页面
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
# Settings for a TLS enabled server.
#	
#	server {
#	    listen       443 ssl http2 default_server;
#	    listen       [::]:443 ssl http2 default_server;
#	    server_name  _;
#	    root         /usr/share/nginx/html;
#	
#	    ssl_certificate "/etc/pki/nginx/server.crt";
#	    ssl_certificate_key "/etc/pki/nginx/private/server.key";
#	    ssl_session_cache shared:SSL:1m;
#	    ssl_session_timeout  10m;
#	    ssl_ciphers HIGH:!aNULL:!MD5;
#	    ssl_prefer_server_ciphers on;
#	
#	    # Load configuration files for the default server block.
#	    include /etc/nginx/default.d/*.conf;
#	
#	    location / {
#	    }
#	
#	    error_page 404 /404.html;
#	        location = /40x.html {
#	    }
#	
#	    error_page 500 502 503 504 /50x.html;
#	        location = /50x.html {
#	    }
#	}

location 的指令作用:

  匹配指定的请求URI

语法:

  location [=|~|~*|^~|@] /uri/ {configuration}

匹配命令:

=   进行普通字符精确匹配
~   表示执行一个正则匹配,区分大小写
~*  表示执行一个正则匹配,不区分大小写
^~  表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配其他。一般用来匹配目录
@   定义一个命名的location,使用在内部定向时,例如error_page,try_files
无前缀 用于普通字符串

优先级:

  1、先匹配精确普通字符串,将最精确的匹配暂时存储;如果没有匹配上,进行普通字符串匹配,若匹配多个,按最长匹配,之后是^~匹配;
  2、然后按照配置文件中的声明顺序进行正则表达式匹配,只要匹配到一个正则表达式,则停止匹配,取正则表达式为匹配结果;
  3、如果所有正则表达式都匹配不上,则取1中的结果;
  4、最后匹配“/”,如果普通字符串和正则表达式都匹配不上,则报404 NOT FOUND。

  "="  > "完整路径" > "^~" > "~/~*" > "/"
	#可以匹配所有请求
	location / {
	}
	
	#只有当用户请求是/时,才会使用该location下的配置
	location = / {
	}
	
	#匹配以/images/开头的任何查询并停止搜索,表示匹配URL时忽略字母大小写问题
	location ^~ /images/ {
	}
	
	#匹配任何以gif、jpg或jpeg结尾的请求。
	location ~* \.(gif|jpg|jpeg)$ {
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盗版摩羯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值