Example for an API Key generator written in PHP. The key that is generated will be 32 non-cryptographic random characters long, and can contain 0-9, a-z (lowercase), A-Z (uppercase). Adding the option for the characters to repeat, creates over 450 quadrillion combinations.
Apr 10, 2020 Create a fresh Laravel Installation. Composer create-project -prefer-dist laravel/laravel blog Setup application key php artisan key:generate Run application in browser php artisan serv Follow this for installing MongoDB. Install composer package in Laravel composer require jenssegers/mongodb Add this in config/app.php JenssegersMongodbMongodbServiceProvider::class. Oct 27, 2019 Keep this very secure. # If you generate a new one all existing data must be considered LOST. # Change it to a string of exactly 32 chars or use command `php artisan key:generate` to generate it APPKEY=key # Change this value to your preferred time zone.
To keep the code short, I generate a random number using rand(48, 122). This number will then be filtered for the ranges of 58 to 64, and 91 to 96. If the random number is present in the previous ranges, the number must be discarded and then recreated. This is done until a number is generated outside of the previous ranges, and this in turn must be completed 32 times. This is done so that the random number can convert into ASCII code (i.e. 0 ; = 0, whitespace added the prevent conversion) to generate the characters mentioned above.
Aug 09, 2018 Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. You can generate a key by the following command: php artisan key:generate The key will be written automatically in your.env file. APPKEY=YOURGENERATEDKEY If you want to see your key after generation use -show option. Php artisan key:generate -show.
Another option would be to create a random number using rand(0, 61). Then using a switch statement append a string together based upon the result. This method results in code roughly 133 lines in length (excluding comments, but allowing whitespace), while the previous method is 27 lines in length (again excluding comments, but allowing whitespace).
Running example at: http://kevinkabatra.ignorelist.com/examples/api%20key%20generator/example_api_key_generator.php
-->Note
This article deploys an app to App Service on Linux. To deploy to App Service on Windows, see Build a PHP and MySQL app in Azure.
App Service on Linux provides a highly scalable, self-patching web hosting service using the Linux operating system. This tutorial shows how to create a PHP app and connect it to a MySQL database. When you're finished, you'll have a Laravel app running on App Service on Linux.
In this tutorial, you learn how to:
If you don't have an Azure subscription, create a free account before you begin.
To complete this tutorial:
In this step, you create a database in your local MySQL server for your use in this tutorial.
In a terminal window, connect to your local MySQL server. You can use this terminal window to run all the commands in this tutorial.
If you're prompted for a password, enter the password for the root
account. If you don't remember your root account password, see MySQL: How to Reset the Root Password.
If your command runs successfully, then your MySQL server is running. If not, make sure that your local MySQL server is started by following the MySQL post-installation steps.
At the mysql
prompt, create a database.
Exit your server connection by typing quit
.
In this step, you get a Laravel sample application, configure its database connection, and run it locally.
In the terminal window, cd
to a working directory.
Run the following command to clone the sample repository.
cd
to your cloned directory.Install the required packages.
In the repository root, create a file named .env. Copy the following variables into the .env file. Replace the <root_password> placeholder with the MySQL root user's password.
For information on how Laravel uses the .env file, see Laravel Environment Configuration.
Run Laravel database migrations to create the tables the application needs. To see which tables are created in the migrations, look in the database/migrations directory in the Git repository.
Generate a new Laravel application key.
Run the application.
Navigate to http://localhost:8000
in a browser. Add a few tasks in the page.
To stop PHP, type Ctrl + C
in the terminal.
Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.
To start Azure Cloud Shell:
Option | Example/Link |
---|---|
Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell. | |
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. | |
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. |
To run the code in this article in Azure Cloud Shell:
Start Cloud Shell.
Select the Copy button on a code block to copy the code.
Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.
Select Enter to run the code.
In this step, you create a MySQL database in Azure Database for MySQL. Later, you configure the PHP application to connect to this database.
A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed. For example, you can choose to delete the entire resource group in one simple step later.
In the Cloud Shell, create a resource group with the az group create
command. The following example creates a resource group named myResourceGroup in the West Europe location. To see all supported locations for App Service on Linux in Basic tier, run the az appservice list-locations --sku B1 --linux-workers-enabled
command.
You generally create your resource group and the resources in a region near you.
When the command finishes, a JSON output shows you the resource group properties.
Create a server in Azure Database for MySQL with the az mysql server create
command.
In the following command, substitute a unique server name for the <mysql-server-name> placeholder, a user name for the <admin-user>, and a password for the <admin-password> placeholder. The server name is used as part of your MySQL endpoint (https://<mysql-server-name>.mysql.database.azure.com
), so the name needs to be unique across all servers in Azure. For details on selecting MySQL DB SKU, please see Create an Azure Database for MySQL server.
When the MySQL server is created, the Azure CLI shows information similar to the following example:
Create a firewall rule for your MySQL server to allow client connections by using the az mysql server firewall-rule create
command. When both starting IP and end IP are set to 0.0.0.0, the firewall is only opened for other Azure resources.
Tip
You can be even more restrictive in your firewall rule by using only the outbound IP addresses your app uses.
In the Cloud Shell, run the command again to allow access from your local computer by replacing <your-ip-address> with your local IPv4 IP address.
In the terminal window, connect to the MySQL server in Azure. Use the value you specified previously for <admin-user> and <mysql-server-name>. When prompted for a password, use the password you specified when you created the database in Azure.
At the mysql
prompt, create a database.
Create a database user called phpappuser and give it all privileges in the sampledb
database.
Exit the server connection by typing quit
.
In this step, you connect the PHP application to the MySQL database you created in Azure Database for MySQL.
In the repository root, create an .env.production file and copy the following variables into it. Replace the placeholder <mysql-server-name>.
Save the changes.
Tip
To secure your MySQL connection information, this file is already excluded from the Git repository (See .gitignore in the repository root). Later, you learn how to configure environment variables in App Service to connect to your database in Azure Database for MySQL. With environment variables, you don't need the .env file in App Service.
By default, Azure Database for MySQL enforces TLS connections from clients. To connect to your MySQL database in Azure, you must use the .pem certificate supplied by Azure Database for MySQL.
Open config/database.php and add the sslmode and options parameters to connections.mysql
, as shown in the following code.
The certificate BaltimoreCyberTrustRoot.crt.pem
is provided in the repository for convenience in this tutorial.
Run Laravel database migrations with .env.production as the environment file to create the tables in your MySQL database in Azure Database for MySQL. Remember that .env.production has the connection information to your MySQL database in Azure.
.env.production doesn't have a valid application key yet. Generate a new one for it in the terminal.
Run the sample application with .env.production as the environment file.
Navigate to http://localhost:8000
. If the page loads without errors, the PHP application is connecting to the MySQL database in Azure.
Add a few tasks in the page.
To stop PHP, type Ctrl + C
in the terminal.
Run the following Git commands to commit your changes:
Your app is ready to be deployed.
In this step, you deploy the MySQL-connected PHP application to Azure App Service.
The Laravel application starts in the /public directory. The default PHP Docker image for App Service uses Apache, and it doesn't let you customize the DocumentRoot
for Laravel. However, you can use .htaccess
to rewrite all requests to point to /public instead of the root directory. In the repository root, an .htaccess
is added already for this purpose. With it, your Laravel application is ready to be deployed.
For more information, see Change site root.
FTP and local Git can deploy to an Azure web app by using a deployment user. Once you configure your deployment user, you can use it for all your Azure deployments. Your account-level deployment username and password are different from your Azure subscription credentials.
To configure the deployment user, run the az webapp deployment user set command in Azure Cloud Shell. Replace <username> and <password> with a deployment user username and password.
The JSON output shows the password as null
. If you get a 'Conflict'. Details: 409
error, change the username. If you get a 'Bad Request'. Details: 400
error, use a stronger password.
Record your username and password to use to deploy your web apps.
In the Cloud Shell, create an App Service plan in the resource group with the az appservice plan create
command.
The following example creates an App Service plan named myAppServicePlan
in the Free pricing tier (--sku F1
) and in a Linux container (--is-linux
).
When the App Service plan has been created, the Azure CLI shows information similar to the following example:
Create a web app in the myAppServicePlan
App Service plan.
In the Cloud Shell, you can use the az webapp create
command. In the following example, replace <app-name>
with a globally unique app name (valid characters are a-z
, 0-9
, and -
). The runtime is set to PHP 7.0
. To see all supported runtimes, run az webapp list-runtimes --linux
.
When the web app has been created, the Azure CLI shows output similar to the following example:
You’ve created an empty new web app, with git deployment enabled.
Note
The URL of the Git remote is shown in the deploymentLocalGitUrl
property, with the format https://<username>@<app-name>.scm.azurewebsites.net/<app-name>.git
. Save this URL as you need it later.
In App Service, you set environment variables as app settings by using the az webapp config appsettings set
command.
The following command configures the app settings DB_HOST
, DB_DATABASE
, DB_USERNAME
, and DB_PASSWORD
. Replace the placeholders <appname> and <mysql-server-name>.
You can use the PHP getenv method to access the app settings. The Laravel code uses an env wrapper over the PHP getenv
. For example, the MySQL configuration in config/database.php looks like the following code:
Laravel needs an application key in App Service. You can configure it with app settings.
Use php artisan
to generate a new application key without saving it to .env.
Set the application key in the App Service app by using the az webapp config appsettings set
command. Replace the placeholders <appname> and <outputofphpartisankey:generate>.
APP_DEBUG='true'
tells Laravel to return debugging information when the deployed app encounters errors. When running a production application, set it to false
, which is more secure.
Add an Azure remote to your local Git repository.
Push to the Azure remote to deploy the PHP application. You are prompted for the password you supplied earlier as part of the creation of the deployment user.
During deployment, Azure App Service communicates its progress with Git.
Browse to http://<app-name>.azurewebsites.net
and add a few tasks to the list.
Congratulations, you're running at see console logs immediately, check again in 30 seconds.
Note
You can also inspect the log files from the browser at https://<app-name>.scm.azurewebsites.net/api/logs/docker
.
To stop log streaming at any time, type Ctrl
+C
.
Go to the Azure portal to manage the app you created.
From the left menu, click App Services, and then click the name of your Azure app.
You see your app's Overview page. Here, you can perform basic management tasks like stop, start, restart, browse, and delete.
The left menu provides pages for configuring your app.
In the preceding steps, you created Azure resources in a resource group. If you don't expect to need these resources in the future, delete the resource group by running the following command in the Cloud Shell:
This command may take a minute to run.
In this tutorial, you learned how to:
Advance to the next tutorial to learn how to map a custom DNS name to your app.
Or, check out other resources: