{"id":144137,"date":"2024-11-01T08:36:41","date_gmt":"2024-11-01T00:36:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/144137\/"},"modified":"2024-11-01T08:36:43","modified_gmt":"2024-11-01T00:36:43","slug":"ubuntu-%e8%bc%95%e9%ac%86%e5%ae%89%e8%a3%9d-php-%e5%92%8c-mysql-%e6%95%b8%e6%93%9a%e5%ba%ab-ubuntu-%e5%ae%89%e8%a3%9d-php-mysql-%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/144137\/","title":{"rendered":"Ubuntu \u8f15\u9b06\u5b89\u88dd PHP \u548c MySQL \u6578\u64da\u5eab (ubuntu \u5b89\u88dd php mysql \u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"ubuntu-%e8%bc%95%e9%ac%86%e5%ae%89%e8%a3%9d-php-%e5%92%8c-mysql-%e6%95%b8%e6%93%9a%e5%ba%ab-BeZSYqHxwH\">Ubuntu \u8f15\u9b06\u5b89\u88dd PHP \u548c MySQL \u6578\u64da\u5eab<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0cPHP \u548c MySQL \u662f\u6700\u53d7\u6b61\u8fce\u7684\u958b\u767c\u6280\u8853\u4e4b\u4e00\u3002\u7121\u8ad6\u662f\u5efa\u7acb\u52d5\u614b\u7db2\u7ad9\u9084\u662f\u958b\u767c\u61c9\u7528\u7a0b\u5e8f\uff0c\u9019\u5169\u8005\u7684\u7d44\u5408\u90fd\u80fd\u63d0\u4f9b\u5f37\u5927\u7684\u529f\u80fd\u3002\u672c\u6587\u5c07\u6307\u5c0e\u60a8\u5982\u4f55\u5728 Ubuntu \u7cfb\u7d71\u4e0a\u8f15\u9b06\u5b89\u88dd PHP \u548c MySQL \u6578\u64da\u5eab\uff0c\u8b93\u60a8\u80fd\u5920\u5feb\u901f\u958b\u59cb\u60a8\u7684\u958b\u767c\u4e4b\u65c5\u3002<\/p>\n<h2 id=\"%e5%89%8d%e6%9c%9f%e6%ba%96%e5%82%99-BeZSYqHxwH\">\u524d\u671f\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u5b89\u88dd\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u60a8\u7684 Ubuntu \u7cfb\u7d71\u5df2\u7d93\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\u3002\u60a8\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u4f86\u66f4\u65b0\u7cfb\u7d71\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt upgrade<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u60a8\u64c1\u6709\u6700\u65b0\u7684\u5b89\u5168\u6027\u548c\u529f\u80fd\u66f4\u65b0\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d-mysql-%e6%95%b8%e6%93%9a%e5%ba%ab-BeZSYqHxwH\">\u5b89\u88dd MySQL \u6578\u64da\u5eab<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b89\u88dd MySQL \u6578\u64da\u5eab\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5b89\u88dd MySQL\uff1a<\/p>\n<pre><code>sudo apt install mysql-server<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u555f\u52d5 MySQL \u670d\u52d9\uff1a<\/p>\n<pre><code>sudo systemctl start mysql<\/code><\/pre>\n<p>\u70ba\u4e86\u78ba\u4fdd MySQL \u5728\u7cfb\u7d71\u555f\u52d5\u6642\u81ea\u52d5\u555f\u52d5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo systemctl enable mysql<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u904b\u884c MySQL \u7684\u5b89\u5168\u6027\u8173\u672c\uff0c\u4ee5\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u5b89\u5168\u6027\uff1a<\/p>\n<pre><code>sudo mysql_secure_installation<\/code><\/pre>\n<p>\u9019\u500b\u8173\u672c\u6703\u5f15\u5c0e\u60a8\u8a2d\u7f6e MySQL \u7684\u6839\u5bc6\u78bc\u3001\u522a\u9664\u533f\u540d\u7528\u6236\u3001\u7981\u6b62\u9060\u7a0b\u767b\u9304\u7b49\u5b89\u5168\u8a2d\u7f6e\u3002\u6839\u64da\u63d0\u793a\u9032\u884c\u64cd\u4f5c\u5373\u53ef\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d-php-BeZSYqHxwH\">\u5b89\u88dd PHP<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u5b89\u88dd PHP\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5b89\u88dd PHP \u53ca\u5176\u5e38\u7528\u64f4\u5c55\uff1a<\/p>\n<pre><code>sudo apt install php libapache2-mod-php php-mysql<\/code><\/pre>\n<p>\u9019\u88e1\u6211\u5011\u5b89\u88dd\u4e86 PHP \u548c\u8207 MySQL \u4ea4\u4e92\u6240\u9700\u7684\u64f4\u5c55\u3002\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6aa2\u67e5 PHP \u662f\u5426\u5b89\u88dd\u6210\u529f\uff1a<\/p>\n<pre><code>php -v<\/code><\/pre>\n<p>\u9019\u5c07\u986f\u793a\u60a8\u5b89\u88dd\u7684 PHP \u7248\u672c\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e9%85%8d%e7%bd%ae-apache-%e4%bc%ba%e6%9c%8d%e5%99%a8-BeZSYqHxwH\">\u914d\u7f6e Apache \u4f3a\u670d\u5668<\/h2>\n<p>\u5982\u679c\u60a8\u9084\u6c92\u6709\u5b89\u88dd Apache \u4f3a\u670d\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt install apache2<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u555f\u52d5 Apache \u670d\u52d9\uff1a<\/p>\n<pre><code>sudo systemctl start apache2<\/code><\/pre>\n<p>\u540c\u6a23\uff0c\u70ba\u4e86\u78ba\u4fdd Apache \u5728\u7cfb\u7d71\u555f\u52d5\u6642\u81ea\u52d5\u555f\u52d5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo systemctl enable apache2<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u5728 Apache \u7684\u6839\u76ee\u9304\u4e2d\u5275\u5efa\u4e00\u500b PHP \u6e2c\u8a66\u6587\u4ef6\uff0c\u4ee5\u78ba\u4fdd PHP \u6b63\u5e38\u904b\u884c\u3002\u5728 \/var\/www\/html \u76ee\u9304\u4e0b\u5275\u5efa\u4e00\u500b\u540d\u70ba info.php \u7684\u6587\u4ef6\uff1a<\/p>\n<pre><code>echo &lt;?php phpinfo(); ?&gt; | sudo tee \/var\/www\/html\/info.php<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5728\u700f\u89bd\u5668\u4e2d\u8a2a\u554f http:\/\/your_server_ip\/info.php\uff0c\u67e5\u770b PHP \u7684\u914d\u7f6e\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e6%b8%ac%e8%a9%a6-mysql-%e5%92%8c-php-%e7%9a%84%e9%80%a3%e6%8e%a5-BeZSYqHxwH\">\u6e2c\u8a66 MySQL \u548c PHP \u7684\u9023\u63a5<\/h2>\n<p>\u70ba\u4e86\u78ba\u4fdd PHP \u80fd\u5920\u6210\u529f\u9023\u63a5\u5230 MySQL \u6578\u64da\u5eab\uff0c\u60a8\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u7684 PHP \u8173\u672c\u4f86\u6e2c\u8a66\u9023\u63a5\u3002\u5728 \/var\/www\/html \u76ee\u9304\u4e0b\u5275\u5efa\u4e00\u500b\u540d\u70ba test_db.php \u7684\u6587\u4ef6\uff1a<\/p>\n<pre><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\"; \/\/ \u6839\u64da\u60a8\u7684\u8a2d\u7f6e\u4fee\u6539\n$password = \"\"; \/\/ \u6839\u64da\u60a8\u7684\u8a2d\u7f6e\u4fee\u6539\n\n\/\/ \u5275\u5efa\u9023\u63a5\n$conn = new mysqli($servername, $username, $password);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\necho \"\u9023\u63a5\u6210\u529f\";\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<p>\u7136\u5f8c\u5728\u700f\u89bd\u5668\u4e2d\u8a2a\u554f http:\/\/your_server_ip\/test_db.php\uff0c\u60a8\u61c9\u8a72\u6703\u770b\u5230\u201c\u9023\u63a5\u6210\u529f\u201d\u7684\u6d88\u606f\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-BeZSYqHxwH\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u5df2\u7d93\u6210\u529f\u5728 Ubuntu \u7cfb\u7d71\u4e0a\u5b89\u88dd\u4e86 PHP \u548c MySQL \u6578\u64da\u5eab\u3002\u9019\u70ba\u60a8\u958b\u767c\u52d5\u614b\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u5960\u5b9a\u4e86\u57fa\u790e\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u9ad8\u6548\u7684\u958b\u767c\u74b0\u5883\uff0c\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u63d0\u5347\u60a8\u7684\u7db2\u7ad9\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002\u7121\u8ad6\u662f\u500b\u4eba\u9805\u76ee\u9084\u662f\u5546\u696d\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u89e3\u6c7a\u65b9\u6848\u90fd\u80fd\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u5be6\u73fe\u76ee\u6a19\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Ubuntu \u4e0a\u8f15\u9b06\u5b89\u88dd PHP \u548c MySQL \u6578\u64da\u5eab\uff0c\u5feb\u901f\u642d\u5efa\u4f60\u7684\u958b\u767c\u74b0\u5883\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-144137","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/144137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=144137"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/144137\/revisions"}],"predecessor-version":[{"id":144138,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/144137\/revisions\/144138"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=144137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=144137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=144137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}