Install nginx, php 7, nginx with php and mysql on windows 7

Installing a website server using a distribution package like XAMPPhường., and WinNMP are probably the easiest solution to make your localhost server works. But what if we wanted lớn make it from scratch? and avoid duplication whenever we install a composer, laravel và other development kit. Also, you’re không lấy phí khổng lồ nâng cấp them whenever a new version comes out.

Bạn đang xem: Install nginx, php 7, nginx with php and mysql on windows 7

Table of Contents

MySQL

If you’re not running the lakiểm tra Windows 10 updates, you need to install the version of VC_Redist you can get it from at https://tư vấn.microsoft.com/en-us/topic/the-latest-supported-visual-c-downloads-2647da03-1eea-4433-9aff-95f26a218cc0, then look for Visual Studio năm ngoái, 2017 & 2019, then tải về your either vc_redist.x86.exe or vc_redist.x64.exe. If you don’t have that, you’ll get this error message when using the PHP..

The code execution cannot proceed because VCRUNTIME140.dll was not found. Reinstalling the program may fix this problem.or if you have a lower version of VC_Redist installed, you’ll get this error.

"vcruntime140.dll" 14.0 is not compatible with this PHP build linked with 14.16 in Unknown on line 0 - Google SearchThat’s for installing the prerequisite. Let’s now install the lachạy thử version of NGINX 1.19.6 + PHP 8.0+ MySquốc lộ 8.0.22.

Nginx

To install nginx, you need lớn tải về it from http://nginx.org/en/download.html. Select the Mainline version zip files. Currently, it’s nginx/Windows-1.19.6.

Once downloaded, create a thư mục in your computer. You may follow our naming convention or create your own. In our case we create a thư mục at C:WebServer.

After created a thư mục, create again a folder for our Nginx. It should be C:WebServer ginx.

Unzip the downloaded nginx-1.19.6.zip at C:WebServer ginx folder.

It’s all good for now, we can discuss later how we activate it.

PHP

To get binaries installation for PHP on Windows, you can grab it at https://windows.php.net/download/.

Then at the PHPhường 8.0, download the VS16 x64 Non Thread Safe zip tệp tin, so we can use it alongside with our Nginx và MySQL.

*
*
*
*
*
*
*
*
*
Once done, clichồng OK.

Xem thêm: php get all cookies

To see if it works, try running these commands using the Windows Powershell or Commvà Prompt.

php -vmysqld -Vnginx -vYou should get these output.

Wrap up

Now that we already added them lớn path. Let’s vày some finishing touch.

To make PHP. communicate with our MySQL. At C:WebServerphp, we need khổng lồ add the module in our PHP.. First, rename the php.ini-production lớn php.ini file

After renaming it, open the php.ini file, search for extension=/path/to/extension/mysqli.so & unbình luận the line by removing the semi-colon “;”, then change its value lớn our mysqli dll (Unfortunately, we need to manually add the exact path of the dll because it is causing error when just using the mặc định extension name).

; When the extension library khổng lồ load is not located in the default extension; directory, You may specify an absolute path lớn the library file:;extension=C:WebServerphpextphp_mysqli.dllYou can then also enable other module extension (DLLs) that are needed for your program. Like the following that are needed for WordPress.

extension=C:WebServerphpextphp_curl.dllextension=C:WebServerphpextphp_pdo_mysql.dllextension=C:WebServerphpextphp_mbstring.dllextension=C:WebServerphpextphp_gd.dllextension=C:WebServerphpexthp_soap.dllWe also need to lớn edit our loadable extensions module khổng lồ avoid PHP. Warning for our MySquốc lộ. Search and find the ;extension_dir = "ext" then unbình luận & replace the value with extension_dir = "c:WebServerphpext". (Credits to Mohamed for this tip)

; Directory in which the loadable extensions (modules) reside.; http://php.net/extension-dir;extension_dir = "./"; On windows:extension_dir = "c:WebServerphpext"In the next step, we will be making a batch file. But there is a problem, running nginx và php-cgi in the batch comm& line will not allow us khổng lồ execute the next line of code. Simply because the program is not yet closed for it to proceed on to the next line.

In order for us to run each lines of code of our batch tệp tin, we need lớn hide the command prompt windows by using RunHiddenConsole, you can download this at their official trang web (https://redmine.lighttpd.net/attachments/660/RunHiddenConsole.zip).

At C:WebServer ginx, unzip the RunHiddenConsole.zip.

Then still at C:WebServer ginx, we need lớn create a batch file called C:WebServer ginxmyhệ thống.bat. This is where we will start & stop our nginx & php-cgi by using a single commvà line. Add the following code. (Thank you khổng lồ Mohamed for enhancing this code).


eđến offcd C:WebServer ginxIF "%1" == "stop" (GOTO STOPSERVER)else IF "%1" == "start" (GOTO STARTSERVER)else (eđến Use these commands:emang lại.emang lại myhệ thống starteđến myhệ thống stop)GOTO END:STARTSERVERQPROCESS * | find /I /N "mysqld.exe">NULIF "%ERRORLEVEL%"=="0" (emang đến MYSQLD is already running.)else (RunHiddenConsole.exe cộ mysqld --consoleemang đến MYSQLD is now running.)QPROCESS * | find /I /N "nginx.exe">NULIF "%ERRORLEVEL%"=="0" (echo NGINX is already running.)else (RunHiddenConsole.exe pháo nginxemang lại NGINX is now running.)QPROCESS * | find /I /N "php-cgi.exe">NULIF "%ERRORLEVEL%"=="0" (emang đến PHP-CGI is already running.)else (RunHiddenConsole.exe cộ php-cgi -b 127.0.0.1:9000eđến PHP-CGI is now running.)echo.emang lại To stop, type "myhệ thống stop"GOTO END:STOPSERVERQPROCESS * | find /I /N "mysqld.exe">NULIF "%ERRORLEVEL%"=="0" (taskkill /F /IM mysqld.exe>NULecho MYSQLD ended successfully.)else (eđến MYSQLD is not running)QPROCESS * | find /I /N "nginx.exe">NULIF "%ERRORLEVEL%"=="0" (::nginx -s stoptaskkill /F /IM nginx.exe>NULemang đến NGINX ended successfully.)else (echo NGINX is not running)QPROCESS * | find /I /N "php-cgi.exe">NULIF "%ERRORLEVEL%"=="0" (taskkill /F /IM php-cgi.exe>NULemang đến PHP-CGI ended successfully.)else (emang lại PHP-CGI is not running):ENDAfter that, you can now use these commands for starting & stopping your hệ thống at the Command Prompt.

myhệ thống start – It will start Nginx, and PHPmyVPS stop – It will stop all instances of Nginx và PHP

But before running the commandline above, we have lớn edit some blocks on our C:/WebServer/nginx/conf/nginx.conf.

First we have to lớn make sure index.php is been recognized by our nginx. find the location / block & change it with the following:

location / root html; index index.php index.html index.htm; Next is we have to make sure nginx know where lớn run the php files. Find and unphản hồi the bloông chồng that says location ~ .php$ , the one that says “pass the PHPhường. scripts to FastCGI VPS listening on 127.0.0.1:9000”. Change it with the following:

location ~ .php$ root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; Once everything is good. Create a tệp tin in C:WebServer ginxhtml esting.php. Then write this sample PHP codes with MySQL connection checker (Make sure to lớn fill up your MySQL login, the mặc định username is root).

";$servername = "localhost";$username = "root";$password = "";// Create connection$conn = mysqli_connect($servername, $username, $password);// Check connectionif (!$conn) die("Connection failed: " . mysqli_connect_error());emang lại "Connected lớn MYSquốc lộ successfully";?> Then at the command prompt. Let’s start our VPS. (Do not forget to Allow Access when Windows Defender Firewall is asking for permission.)

myserver startYou should get example output below.

MYSQLD is now running.NGINX is now running.PHP-CGI is now running.To stop, type "myhệ thống stop"Now to lớn really kiểm tra its indeed running. Cheông xã at your browser & access https://localhost/testing.php.

PHPhường works!Connected to lớn MYSquốc lộ successfully
NULIF "%ERRORLEVEL%"=="0" (emang lại MYSQLD is already running.)else (RunHiddenConsole.exe mysqld --consoleeđến MYSQLD is now running.)QPROCESS * | find /I /N "nginx.exe">NULIF "%ERRORLEVEL%"=="0" (emang đến NGINX is already running.)else (RunHiddenConsole.exe cộ nginxemang đến NGINX is now running.)QPROCESS * | find /I /N "php-cgi.exe">NULIF "%ERRORLEVEL%"=="0" (echo PHP-CGI is already running.)else (RunHiddenConsole.exe php-cgi -b 127.0.0.1:9000emang đến PHP-CGI is now running.)eđến.echo To stop, type "myserver stop"GOTO END:STOPSERVERQPROCESS * | find /I /N "mysqld.exe">NULIF "%ERRORLEVEL%"=="0" (taskkill /F /IM mysqld.exe>NULemang đến MYSQLD ended successfully.)else (emang đến MYSQLD is not running)QPROCESS * | find /I /N "nginx.exe">NULIF "%ERRORLEVEL%"=="0" (::nginx -s stoptaskkill /F /IM nginx.exe>NULemang lại NGINX ended successfully.)else (emang đến NGINX is not running)QPROCESS * | find /I /N "php-cgi.exe">NULIF "%ERRORLEVEL%"=="0" (taskkill /F /IM php-cgi.exe>NULeđến PHP-CGI ended successfully.)else (eđến PHP-CGI is not running):ENDAfter that, you can now use these commands for starting và stopping your VPS at the Command Prompt.myVPS start - It will start Nginx, and PHPmyVPS stop - It will stop all instances of Nginx & PHPBut before running the commandline above, we have to lớn edit some blocks on our C:/WebServer/nginx/conf/nginx.conf. First we have sầu lớn make sure index.php is been recognized by our nginx. find the location / blochồng và change it with the following: location / root html; index index.php index.html index.htm; Next is we have sầu to make sure nginx know where khổng lồ run the php files. Find and uncomment the block that says location ~ .php$ , the one that says "pass the PHP.. scripts to lớn FastCGI hệ thống listening on 127.0.0.1:9000". Change it with the following: location ~ .php$ root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; Once everything is good. Create a tệp tin in C:WebServer ginxhtml esting.php. Then write this sample PHPhường codes with MySquốc lộ connection checker (Make sure lớn fill up your MySQL login, the default username is root).";$servername = "localhost";$username = "root";$password = "";// Create connection$conn = mysqli_connect($servername, $username, $password);// Cheông xã connectionif (!$conn) die("Connection failed: " . mysqli_connect_error());emang đến "Connected to lớn MYSQL successfully";?> Then at the command prompt. Let's start our VPS. (Do not forget khổng lồ Allow Access when Windows Defender Firewall is asking for permission.)myhệ thống startYou should get example output below.MYSQLD is now running.NGINX is now running.PHP-CGI is now running.To stop, type "myserver stop"Now lớn really kiểm tra its indeed running. Chechồng at your browser and access https://localhost/testing.php.PHPhường works!Connected to lớn MYSquốc lộ successfully" data-count="tumblr">