ubuntu20+PHP项目运行环境搭建

文章目录

  • ubuntu20(root)+PHP+nginx+mysql
    • nginx【较新版】
    • PHP7.4【修改数字即可安装不同版本】
    • mysql8
      • mysql8设置密码
    • 整合PHP+nginx
    • php连接sqlserver配置
      • ODBC驱动安装
      • PHP扩展【sqlsrv】安装
      • 连接测试

ubuntu20(root)+PHP+nginx+mysql

nginx【较新版】

wget http://nginx.org/keys/nginx_signing.key

apt-key add nginx_signing.key

lsb_release -a

vi /etc/apt/sources.list

deb http://nginx.org/packages/ubuntu/ focal nginx
deb-src http://nginx.org/packages/ubuntu/ focal nginx

apt update

apt -y install nginx

systemctl enable/disable/status/start/stop/restart nginx

nginx -v

1.20.1

PHP7.4【修改数字即可安装不同版本】

aptitude purge dpkg -l | grep php| awk '{print $2}' |tr "\n" " " // 彻底卸载apt安装的PHP

apt -y install software-properties-common

add-apt-repository ppa:ondrej/php

apt update

apt -y install php7.4 php7.4-fpm

apt -y install php7.4-mcrypt php7.4-mbstring php7.4-curl php7.4-cli php7.4-mysql php7.4-gd php7.4-intl php7.4-xsl php7.4-zip php7.4-dev php7.4-xml

systemctl enable/disable/status/start/stop/restart php7.4-fpm

php -v

7.4.22

mysql8

apt install -y nginx
apt install -y mysql-server

mysql8设置密码

第一步:mysql8默认安装完成后,root账户没有密码,直接执行命令“mysql”即可登录。

第二步:还原密码设置。

update mysql.user set authentication_string=‘’ where user=‘root’;
update mysql.user set plugin=‘mysql_native_password’ where user=‘root’;
FLUSH PRIVILEGES;

第三步:通过mysql提供的脚本来设置密码。
MySQL安装随附一个名为的脚本mysql_secure_installation。可以借助它设置我们MYSQL服务器的安全性!

sudo mysql_secure_installation
按提示执行即可。最后出现“All done!”说明脚本执行完成。直接使用新设置密码登录即可。

整合PHP+nginx

find / -name www.conf

vi /etc/php/7.4/fpm/pool.d/www.conf

// 设置php-fpm通过监听端口方式工作
;listen = /run/php/php7.4-fpm.sock
listen = 127.0.0.1:9000

ps -ef|grep nginx

// nginx配置文件地址
root      498847       1  0 00:42 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

vi /etc/nginx/nginx.conf

// 自定义配置文件位置
include /etc/nginx/conf.d/*.conf;

cd /etc/nginx/conf.d/

vi weiweixiao.conf

:wq

## weiweixiao.conf内容
server {
		# 修改真实域名端口
		# 如果就是使用默认80端口,就将此配置移植到/etc/nginx/sites-enable/default中
        listen 80 default_server;
        # listen [::]:80 default_server ipv6only=on;

		# 修改真实项目地址
        root /home/huashi_root/www/weiweixiaov2_simp;
        index index.html index.htm index.php;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
                index index.html index.htm index.php;
		        #       if (!-e $request_filename) {
		        #               break;
		        #       }
				#       try_files $uri /index.php$uri;
				# 伪静态配置
                if (!-e $request_filename) {
                        rewrite ^/(.*)$ /index.php/$1 last;
                        break;
                }
        }

		# php-fpm配置
        location ~ .+\.php($|/) {
                fastcgi_index index.php;
        		# fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
                # 监听php-fpm端口
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_connect_timeout 300s;
                fastcgi_send_timeout 300s;
                fastcgi_read_timeout 300s;
                include fastcgi_params;
        }

        location ^~ /Application/Runtime {
                deny all;
        }

        location ~* .*\.(svn|git|cvs).* {
                deny all;
        }

}

php连接sqlserver配置

ODBC驱动安装

官网教程:https://learn.microsoft.com/zh-cn/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16#ubuntu17

# 查询Ubuntu系统版本号
lsb_release -rs  # 20.04
# 安装ODBC驱动
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
apt-get update
ACCEPT_EULA=Y apt-get install -y msodbcsql18
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev

PHP扩展【sqlsrv】安装

官网教程:https://learn.microsoft.com/zh-cn/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver16

# 如果,提示不存在pecl,按照提示 安装即可。

pecl install sqlsrv
pecl install pdo_sqlsrv
printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/7.4/mods-available/sqlsrv.ini
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/7.4/mods-available/pdo_sqlsrv.ini
phpenmod -v 7.4 sqlsrv pdo_sqlsrv
systemctl resrart php7.4-fpm

连接测试


 'sqlsrv',
        'db_user'  => 'user',
        'db_pwd'   => "password",
        'db_host'  => '127.0.0.1',
        'db_port'  => '1433',
        'db_name'  => 'dbname',
        'db_charset' => 'UTF8',
    );

   public function test(){
       $sql='select * from table_name where id =1';
	   dump($this->querySql($sql));
   }

    //region sqlsrv直连

    protected $sqlsrvConn = null;
    protected function sqlsrvConn(){
        $dbServerName = $this->DB_SQLSERVER['db_host'] . "," . $this->DB_SQLSERVER['db_port'];
        $connInfo = array();
        $connInfo['UID'] = $this->DB_SQLSERVER['db_user'];
        $connInfo['PWD'] = $this->DB_SQLSERVER['db_pwd'];
        $connInfo['Database'] = $this->DB_SQLSERVER['db_name'];
        $connInfo['Encrypt'] = 'no'; // 不使用SSL加密
        $this->sqlsrvConn = sqlsrv_connect($dbServerName,$connInfo);
        if($this->sqlsrvConn === false){
            $this->ajaxReturn(genInfoTip(CODE_FAIL, "sqlserver数据库连接失败:".sqlsrv_errors()));
        }
    }
    public function querySql($sql){
        if(empty($sql)){
            return genInfoTip(CODE_FAIL, "sql不能为空");
        }
        $this->sqlsrvConn();
        $queryRet = sqlsrv_query($this->sqlsrvConn, $sql);
        if($queryRet === false){
            $this->ajaxReturn(genInfoTip(CODE_FAIL, "sqlserver数据库查询异常:".sqlsrv_errors()));
        }
        $result = array();
        while(($row = sqlsrv_fetch_array($queryRet))){
            $result[] = $row;
        }
        return $result;
    }
    public function execSql($sql){
        if(empty($sql)){
            return genInfoTip(CODE_FAIL, "sql不能为空");
        }
        $this->sqlsrvConn();
        $exec = sqlsrv_query($this->sqlsrvConn, $sql);
        if($exec === false){
            $this->ajaxReturn(genInfoTip(CODE_FAIL, "sqlserver数据库sql操作异常:".sqlsrv_errors()));
        }
        $num=sqlsrv_rows_affected($exec);//返回修改的行数
        return $num;
    }

    // endregion
}

你可能感兴趣的:(环境搭建,php,mysql,数据库)