一、服务器从零到一——Ubuntu搭建nginx静态服务器

一、服务器从零到一——Ubuntu搭建nginx静态服务器,第1张

Ubuntu 18.04上的Nginx默认启用了一个服务器模块,该模块被配置为在/var/www/html目录下提供文档。 虽然这适用于单个站点,但如果您托管多个站点,它可能会变得很笨重。 我们不必修改/var/www/html ,而是在/var/www为我们的 example.com 网站创建一个目录结构,并将/var/www/html保留为默认目录,如果客户端请求没有匹配任何其他网站。

按如下所示为example.com创建目录,使用-p标志创建任何必需的父目录:

$ sudo mkdir -p /var/www/ example.com/html

接下来,使用$USER环境变量分配目录的所有权:

USER:$USER /var/www/ example.com/html/

如果你没有修改你的umask值,你的web根目录的权限应该是正确的,但是你可以通过输入:

$ sudo chmod -R 755 /var/www/ example.com/

接下来,使用gedit或您最喜欢的编辑器创建一个index.html页面示例:

$ gedit /var/www/ example.com/html/index.html

在里面,添加下面的示例HTML:

<html>

<head>

<title>Welcome to Example.com!</title>

</head>

<body>

<h1>Success! The example.com server block is working!</h1>

</body>

</html>

为了让Nginx提供这些内容,有必要创建一个具有正确指令的服务器块。 我们不要直接修改默认配置文件,而是在/etc/nginx/sites-available/ example.com上创建一个新文件:

$ sudo gedit /etc/nginx/sites-available/example.com

粘贴到以下配置块中,该块类似于默认值,但已更新为我们的新目录和域名:

server {

listen 80

listen [::]:80

}

请注意,我们已将root配置更新到我们的新目录,并将server_name为我们的域名。

接下来,让我们通过创建一个链接到启动sites-enabled目录来启用该文件,该目录是Nginx在启动过程中读取的:

$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

现在启用两个服务器模块并将其配置为基于listen和server_name指令响应请求(您可以阅读关于Nginx如何处理这些指令的更多信息):

example.com :将响应 example.com 和 www.example.com 请求。

default :将响应端口80上与其他两个块不匹配的任何请求。

为了避免添加额外的服务器名称可能导致的哈希桶内存问题,有必要调整/etc/nginx/nginx.conf文件中的单个值。

打开文件:sudo gedit /etc/nginx/nginx.conf

找到server_names_hash_bucket_size指令并删除#符号以取消注释该行:

...

http {

...

server_names_hash_bucket_size 64

...

}

...

接下来,测试以确保您的Nginx文件中没有语法错误:

$ sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

如果没有任何问题,请重新启动Nginx以启用您的更改:

$ sudo systemctl restart nginx

熟悉重要的Nginx文件和目录

nginx服务器配置文件:

/etc/nginx :Nginx配置目录。 所有的Nginx配置文件都驻留在这里。

/etc/nginx/nginx.conf :主要的Nginx配置文件。 这可以修改,以更改Nginx全局配置。

/etc/nginx/sites-available/ :可存储每个站点服务器块的目录。 除非将Nginx链接到sites-enabled了sites-enabled目录,否则Nginx不会使用此目录中的配置文件。 通常,所有服务器块配置都在此目录中完成,然后通过链接到其他目录启用。

/etc/nginx/sites-enabled/ :存储启用的每个站点服务器块的目录。 通常,这些是通过链接到sites-available目录中的配置文件创建的。

/etc/nginx/snippets :这个目录包含可以包含在Nginx配置其他地方的配置片段。 可重复配置的片段可以重构为片段。

nginx服务器日志文件:

/var/log/nginx/access.log :除非Nginx配置为其他方式,否则每个对您的Web服务器的请求都会记录在此日志文件中。

/var/log/nginx/error.log :任何Nginx错误都会记录在这个日志中。

由于后期threeJs需要使用各种外部资源,所以需要搭建服务器,nginx,apache,iis等都可以。

本人则使用nodeJs的koa搭建一个简单的服务器

开发工具使用VSCode

首先需要确保电脑上已经按装了node.js

如果使用VSCode:

Frozen-Flask freezes a Flask application into a set of static files. The result can be hosted without any server-side software other than a traditional web server.

Note: This project used to be called Flask-Static.

Installation

Install the extension with one of the following commands:

$ easy_install Frozen-Flask

or alternatively if you have pip installed:

$ pip install Frozen-Flask

or you can get the source code from github.

Context

This documentation assumes that you already have a working Flask application. You can run it and test it with the development server:

from myapplication import appapp.run(debug=True)

Frozen-Flask is only about deployment: instead of installing Python, a WGSI server and Flask on your server, you can use Frozen-Flask to freeze your application and only have static HTML files on your server.

Getting started

Create a Freezer instance with your app object and call its freeze() method. Put that in a freeze.py script (or call it whatever you like):

from flask_frozen import Freezerfrom myapplication import appfreezer = Freezer(app)if __name__ == '__main__':

freezer.freeze()

This will create a build directory next to your application’s static and templatesdirectories, with your application’s content frozen into  static files.

Note

Frozen-Flask considers it “owns” its build directory. By default, it will silently overwrite files in that directory, and remove those it did not create.

The configuration allows you to change the destination directory, or control what files are removed if at all.

This build will most likely be partial since Frozen-Flask can only guess so much about your application.

Finding URLs

Frozen-Flask works by simulating requests at the WSGI level and writing the responses to aptly named files. So it needs to find out which URLs exist in your application.

The following URLs can be found automatically:

Static files handled by Flask for your application or any of its blueprints.

Views with no variable parts in the URL, if they accept the GET method.

New in version 0.6: Results of calls to flask.url_for() made by your application in the request for another URL. In other words, if you use url_for() to create links in your application, these links will be “followed”.

This means that if your application has an index page at the URL / (without parameters) and every other page can be found from there by recursively following links built with url_for(), then Frozen-Flask can discover all URLs automatically and you’re done.

Otherwise, you may need to write URL generators.

URL generators

Let’s say that your application looks like this:

@app.route('/')def products_list():

   return render_template('index.html', products=models.Product.all())@app.route('/product_<int:product_id>/')def product_details():

   product = models.Product.get_or_404(id=product_id)

   return render_template('product.html', product=product)

If, for some reason, some products pages are not linked from another page (or these links are not built by url_for()), Frozen-Flask will not find them.

To tell Frozen-Flask about them, write an URL generator and put it after creating your Freezer instance and before calling freeze():

@freezer.register_generatordef product_details():

   for product in models.Product.all():

       yield {'product_id': product.id}

Frozen-Flask will find the URL by calling url_for(endpoint, **values) whereendpoint is the name of the generator function and values is each dict yielded by the function.

You can specify a different endpoint by yielding a (endpoint, values) tuple instead of just values, or you can by-pass url_for and simply yield URLs as strings.

Also, generator functions do not have to be Python generators using yield, they can be any callable and return any iterable object.

All of these are thus equivalent:

@freezer.register_generatordef product_details():  # endpoint defaults to the function name

   # `values` dicts

   yield {'product_id': '1'}

   yield {'product_id': '2'}@freezer.register_generatordef product_url_generator():  # Some other function name

   # `(endpoint, values)` tuples

   yield 'product_details', {'product_id': '1'}

   yield 'product_details', {'product_id': '2'}@freezer.register_generatordef product_url_generator():

   # URLs as strings

   yield '/product_1/'

   yield '/product_2/'@freezer.register_generatordef product_url_generator():

   # Return a list. (Any iterable type will do.)

   return [

       '/product_1/',

       # Mixing forms works too.

       ('product_details', {'product_id': '2'}),

   ]

Generating the same URL more than once is okay, Frozen-Flask will build it only once. Having different functions with the same name is generally a bad practice, but still work here as they are only used by their decorators. In practice you will probably have a module for your views and another one for the freezer and URL generators, so having the same name is not a problem.

Testing URL generators

The idea behind Frozen-Flask is that you can use Flask directly to develop and test your application. However, it is also useful to test your URL generators and see that nothing is missing, before deploying to a production server.

You can open the newly generated static HTML files in a web browser, but links probably won’t work. The FREEZER_RELATIVE_URLS configuration can fix this, but adds a visible index.html to the links. Alternatively, use the run() method to start an HTTP server on the build result, so you can check that everything is fine before uploading:

if __name__ == '__main__':

   freezer.run(debug=True)

Freezer.run() will freeze your application before serving and when the reloader kicks in. But the reloader only watches Python files, not templates or static files. Because of that, you probably want to use Freezer.run() only for testing the URL generators. For everything else use the usual app.run().

Flask-Script may come in handy here.

Controlling What Is Followed

Frozen-Flask follows links automatically or with some help from URL generators. If you want to control what gets followed, then URL generators should be used with the Freezer’s with_no_argument_rules and log_url_for flags. Disabling these flags will force Frozen-Flask to use URL generators only. The combination of these three elements determines how much Frozen-Flask will follow.

Configuration

Frozen-Flask can be configured using Flask’s configuration system. The following configuration values are accepted:

FREEZER_BASE_URL

Full URL your application is supposed to be installed at. This affects the output of flask.url_for() for absolute URLs (with _external=True) or if your application is not at the root of its domain name. Defaults to 'http://localhost/'.

FREEZER_RELATIVE_URLS

If set to True, Frozen-Flask will patch the Jinja environment so that url_for() returns relative URLs. Defaults to False. Python code is not affected unless you use relative_url_for() explicitly. This enables the frozen site to be browsed without a web server (opening the files directly in a browser) but appends a visible index.html to URLs that would otherwise end with /.

New in version 0.10.

FREEZER_DEFAULT_MIMETYPE

The MIME type that is assumed when it can not be determined from the filename extension. If you’re using the Apache web server, this should match the DefaultTypevalue of Apache’s configuration.  Defaults to application/octet-stream.

New in version 0.7.

FREEZER_IGNORE_MIMETYPE_WARNINGS

If set to True, Frozen-Flask won’t show warnings if the MIME type returned from the server doesn’t match the MIME type derived from the filename extension. Defaults to False.

New in version 0.8.

FREEZER_DESTINATION

Path to the directory where to put the generated static site. If relative, interpreted as relative to the application root, next to the static and templates directories. Defaults to build.

FREEZER_REMOVE_EXTRA_FILES

If set to True (the default), Frozen-Flask will remove files in the destination directory that were not built during the current freeze. This is intended to clean up files generated by a previous call to Freezer.freeze() that are no longer needed. Setting this to False is equivalent to setting FREEZER_DESTINATION_IGNORE to ['*'].

New in version 0.5.

FREEZER_DESTINATION_IGNORE

A list (defaults empty) of fnmatch patterns. Files or directories in the destination that match any of the patterns are not removed, even if FREEZER_REMOVE_EXTRA_FILES is true. As in .gitignore files, patterns apply to the whole path if they contain a slash /, to each slash-separated part otherwise. For example, this could be set to ['.git


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/224478.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-08
下一篇2023-04-08

发表评论

登录后才能评论

评论列表(0条)

    保存