lnmp環境(ubthun16.04_x64)搭建+倆個多站點(abc、bcd)
初步說明
IP 地址192.168.20.79,主機名localhost。
所有步驟使用root權限,所以一定要確保root身份登錄:
安裝 MySQL 5.7
1.安裝 MySQL 運行命令:
apt-get -y install mysql-server mysql-client
要求提供MySQL的root用戶密碼 :
輸入密碼->table->ok,再輸入一次
為了確保數據庫服務器,并刪除匿名用戶和測試數據庫,運行mysql_secure_installation命令。
mysql_secure_installation
會問這些問題:
root@server1:~# mysql_secure_installation
保護MySQL服務器部署。
Enter password for user root: <– Enter the MySQL root password
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: <– Press y if you want this function or press Enter otherwise.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : <– Press enter
… skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : <– y
Success.
Normally, root should only be allowed to connect from
‘localhost’. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : <– y
Success.
By default, MySQL comes with a database named ‘test’ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : <– y
– Dropping test database…
Success.
– Removing privileges on test database…
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : <– y
Success.
3 安裝 Nginx
如果已經安裝了Apache2的話,那么使用這些命令先刪除再安裝nginx:
刪除apache2:
service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2
Ubuntu16.04有Nginx安裝包,我們可以安裝。
安裝nginx
apt-get -y install nginx
Start nginx afterwards:
重啟nginx:service nginx start
輸入您的Web服務器的IP地址或主機名到瀏覽器(例如http://192.168.20.79),你應該看到如下頁面:
在Ubuntu16.04的默認nginx的文檔根目錄為/var/www/html
4 安裝 PHP 7
我們可以通過使nginx的PHP工作PHP-FPM(PHP-FPM(FastCGI進程管理器)是為任何規模的網站,尤其是繁忙的網站有用的一些附加功能的替代PHP的FastCGI實現),我們安裝如下:
apt-get -y install php7.0-fpm
5 配置 nginx
1.打開配置文件 /etc/nginx/nginx.conf:
vi /etc/nginx/nginx.conf
http://wiki.nginx.org/NginxFullExample 或:http://wiki.nginx.org/NginxFullExample2
首先(這是可選)調整keepalive_timeout到一個合理的值:
[...]
keepalive_timeout 2;
[...]
虛擬主機服務器{}容器定義。默認的虛擬主機是在文件中定義的/etc/nginx/sites-available/default – 讓我們來修改它,如下所示:
vim /etc/nginx/sites-available/default
[...]
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}
}
[...]
server_name _; 使這是一個默認捕捉所有虛擬主機(當然,你可以同時喜歡這里www.example.com指定主機名)。
根目錄 /var/www/html;意味著文檔根目錄/var/www/html.
PHP的重要組成部分位置 ~ .php$ {} stanza. 取消注釋它來啟用它。
現在保存文件并重新加載nginx:
service nginx reload
2.打開 /etc/php/7.0/fpm/php.ini…
vim /etc/php/7.0/fpm/php.ini
設置 cgi.fix_pathinfo=0:
[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]
重新加載 PHP-FPM:
service php7.0-fpm reload
建立探針文件/var/www/html:
vim /var/www/html/info.php
<?php
phpinfo();
?>
瀏覽器訪問 (e.g. http://192.168.20.79/info.php):
6 讓 MySQL 獲得 PHP 7支持
1.先搜索一下PHP支持的模塊:
apt-cache search php7.0
使用下面的命令安裝:
apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext
2.APCu是隨PHP7 PHP Opcache模塊的擴展,它增加了一些兼容性功能的支持APC緩存(例如WordPress的插件緩存)軟件。
APCu可以安裝如下:
apt-get -y install php-apcu
重新加載 PHP-FPM:
service php7.0-fpm reload
刷新 http://192.168.20.79/info.php 瀏覽器看看模塊安裝情況:
The PHP Modules have been installed.
7 讓 PHP-FPM 使用 TCP 連接
1.默認情況下PHP-FPM監聽 /var/run/php/php7.0-fpm.sock. 另外,也可以使 PHP-FPM 試用 TCP 連接,打開文件 /etc/php/7.0/fpm/pool.d/www.conf…
vim /etc/php/7.0/fpm/pool.d/www.conf
修改如下:
[...]
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
[...]
這將使PHP-FPM端口9000偵聽的IP127.0.0.1(本地主機)。請確保您使用的端口,是不是在你的系統上使用。
然后重新加載 PHP-FPM:
php7.0-fpm reload
2.接下來通過你的nginx的配置和所有的虛擬主機,并更改fastcgi_pass UNIX行:/var/run/php/php7.0-fpm.sock; tofastcgi_pass127.0.0.1:9000;,如下:
vim /etc/nginx/sites-available/default
[...]
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
[...]
3.最后,重新加載nginx:
service nginx reload
ok,nginx的lnmp服務器安裝完畢。
開啟倆站點(abc、bcd)
1.復制default并重命名為自己的網站名(abc)
cp default abc
2.設置自己網站站點路徑和域名
設置abc中的root:/var/www/html/abc
設置abc中的域名:servername:www.abc.com
3.創建文件夾/var/www/html/abc,并在其下制作網站
mkdir /var/www/html/abc
vim /var/www/html/abc/index.php
寫入:
<?php
echo "this is abc web";
?>
4.更改windows下的域名設置(C:\Windows\System32\drivers\etc下的hosts)
加一條:192.168.20.79 www.abc.com