lnmp环境(ubthun16.04_x64)搭建+俩个多站点(abc、bcd)

初步说明

IP 地址192.168.20.79,主机名localhost。
所有步骤使用root权限,所以一定要确保root身份登录:

安装 MySQL 5.7

1.安装 MySQL 运行命令:

  1. apt-get -y install mysql-server mysql-client

要求提供MySQL的root用户密码 :
输入密码->table->ok,再输入一次

为了确保数据库服务器,并删除匿名用户和测试数据库,运行mysql_secure_installation命令。

  1. mysql_secure_installation
  2. 会问这些问题:
  3. root@server1:~# mysql_secure_installation
  4. 保护MySQL服务器部署。
  5. Enter password for user root: <– Enter the MySQL root password
  6. VALIDATE PASSWORD PLUGIN can be used to test passwords
  7. and improve security. It checks the strength of password
  8. and allows the users to set only those passwords which are
  9. secure enough. Would you like to setup VALIDATE PASSWORD plugin?
  10. Press y|Y for Yes, any other key for No: <– Press y if you want this function or press Enter otherwise.
  11. Using existing password for root.
  12. Change the password for root ? ((Press y|Y for Yes, any other key for No) : <– Press enter
  13. skipping.
  14. By default, a MySQL installation has an anonymous user,
  15. allowing anyone to log into MySQL without having to have
  16. a user account created for them. This is intended only for
  17. testing, and to make the installation go a bit smoother.
  18. You should remove them before moving into a production
  19. environment.
  20. Remove anonymous users? (Press y|Y for Yes, any other key for No) : <– y
  21. Success.
  22. Normally, root should only be allowed to connect from
  23. localhost’. This ensures that someone cannot guess at
  24. the root password from the network.
  25. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : <– y
  26. Success.
  27. By default, MySQL comes with a database named test that
  28. anyone can access. This is also intended only for testing,
  29. and should be removed before moving into a production
  30. environment.
  31. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : <– y
  32. Dropping test database
  33. Success.
  34. Removing privileges on test database
  35. Success.
  36. Reloading the privilege tables will ensure that all changes
  37. made so far will take effect immediately.
  38. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : <– y
  39. Success.

3 安装 Nginx

如果已经安装了Apache2的话,那么使用这些命令先删除再安装nginx:

  1. 删除apache2:
  2. service apache2 stop
  3. update-rc.d -f apache2 remove
  4. apt-get remove apache2

Ubuntu16.04有Nginx安装包,我们可以安装。

  1. 安装nginx
  2. apt-get -y install nginx
  3. Start nginx afterwards:
  4. 重启nginxservice 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实现),我们安装如下:

  1. apt-get -y install php7.0-fpm

5 配置 nginx

1.打开配置文件 /etc/nginx/nginx.conf:

  1. vi /etc/nginx/nginx.conf

http://wiki.nginx.org/NginxFullExample 或:http://wiki.nginx.org/NginxFullExample2

首先(这是可选)调整keepalive_timeout到一个合理的值:

  1. [...]
  2. keepalive_timeout 2;
  3. [...]

虚拟主机服务器{}容器定义。默认的虚拟主机是在文件中定义的/etc/nginx/sites-available/default – 让我们来修改它,如下所示:

  1. vim /etc/nginx/sites-available/default
  2. [...]
  3. server {
  4. listen 80;
  5. listen [::]:80 default_server ipv6only=on;
  6. root /usr/share/nginx/html;
  7. index index.php index.html index.htm;
  8. # Make site accessible from http://localhost/
  9. server_name _;
  10. location / {
  11. # First attempt to serve request as file, then
  12. # as directory, then fall back to displaying a 404.
  13. try_files $uri $uri/ /index.html;
  14. # Uncomment to enable naxsi on this location
  15. # include /etc/nginx/naxsi.rules
  16. }
  17. location /doc/ {
  18. alias /usr/share/doc/;
  19. autoindex on;
  20. allow 127.0.0.1;
  21. allow ::1;
  22. deny all;
  23. }
  24. # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
  25. #location /RequestDenied {
  26. # proxy_pass http://127.0.0.1:8080;
  27. #}
  28. #error_page 404 /404.html;
  29. # redirect server error pages to the static page /50x.html
  30. #
  31. error_page 500 502 503 504 /50x.html;
  32. location = /50x.html {
  33. root /usr/share/nginx/html;
  34. }
  35. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  36. #
  37. location ~ .php$ {
  38. try_files $uri =404;
  39. fastcgi_split_path_info ^(.+.php)(/.+)$;
  40. # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
  41. # With php5-cgi alone:
  42. #fastcgi_pass 127.0.0.1:9000;
  43. # With php5-fpm:
  44. fastcgi_pass unix:/var/run/php5-fpm.sock;
  45. fastcgi_index index.php;
  46. include fastcgi_params;
  47. }
  48. # deny access to .htaccess files, if Apache's document root
  49. # concurs with nginx's one
  50. #
  51. location ~ /.ht {
  52. deny all;
  53. }
  54. }
  55. [...]

servername ; 使这是一个默认捕捉所有虚拟主机(当然,你可以同时喜欢这里www.example.com指定主机名)。

根目录 /var/www/html;意味着文档根目录/var/www/html.

PHP的重要组成部分位置 ~ .php$ {} stanza. 取消注释它来启用它。

现在保存文件并重新加载nginx:

service nginx reload

2.打开 /etc/php/7.0/fpm/php.ini…

  1. vim /etc/php/7.0/fpm/php.ini
  2. 设置 cgi.fix_pathinfo=0:
  3. [...]
  4. ; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
  5. ; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
  6. ; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
  7. ; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
  8. ; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
  9. ; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
  10. ; http://php.net/cgi.fix-pathinfo
  11. cgi.fix_pathinfo=0
  12. [...]

重新加载 PHP-FPM:

  1. service php7.0-fpm reload

建立探针文件/var/www/html:

  1. vim /var/www/html/info.php
  2. <?php
  3. phpinfo();
  4. ?>
  5. 浏览器访问 (e.g. http://192.168.20.79/info.php):

6 让 MySQL 获得 PHP 7支持

1.先搜索一下PHP支持的模块:

  1. apt-cache search php7.0

使用下面的命令安装:

  1. 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可以安装如下:

  1. apt-get -y install php-apcu

重新加载 PHP-FPM:

  1. 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…

  1. vim /etc/php/7.0/fpm/pool.d/www.conf

修改如下:

  1. [...]
  2. ;listen = /var/run/php5-fpm.sock
  3. listen = 127.0.0.1:9000
  4. [...]

这将使PHP-FPM端口9000侦听的IP127.0.0.1(本地主机)。请确保您使用的端口,是不是在你的系统上使用。

然后重新加载 PHP-FPM:

  1. php7.0-fpm reload

2.接下来通过你的nginx的配置和所有的虚拟主机,并更改fastcgi_pass UNIX行:/var/run/php/php7.0-fpm.sock; tofastcgi_pass127.0.0.1:9000;,如下:

  1. vim /etc/nginx/sites-available/default
  2. [...]
  3. location ~ \.php$ {
  4. include snippets/fastcgi-php.conf;
  5. # With php7.0-cgi alone:
  6. fastcgi_pass 127.0.0.1:9000;
  7. # With php7.0-fpm:
  8. # fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  9. }
  10. [...]

3.最后,重新加载nginx:

  1. service nginx reload

ok,nginx的lnmp服务器安装完毕。

开启俩站点(abc、bcd)

1.复制default并重命名为自己的网站名(abc)

  1. cp default abc

2.设置自己网站站点路径和域名

  1. 设置abc中的root:/var/www/html/abc
  2. 设置abc中的域名:servernamewww.abc.com

3.创建文件夹/var/www/html/abc,并在其下制作网站

  1. mkdir /var/www/html/abc
  2. vim /var/www/html/abc/index.php
  3. 写入:
  4. <?php
  5. echo "this is abc web";
  6. ?>

4.更改windows下的域名设置(C:\Windows\System32\drivers\etc下的hosts)

  1. 加一条:192.168.20.79 www.abc.com

5.打开浏览器,输入域名“www.abc.com”

6.第二个站点(bcd)同abc

您的支持是对我最大的鼓励!

分类: linux

标签:   lnmp