Cloud FEMs · Apache Tomcat is a web server and servlet container that is used to serve Java...
Transcript of Cloud FEMs · Apache Tomcat is a web server and servlet container that is used to serve Java...
1 / 28
目录 How To Install Apache Tomcat 8 on Ubuntu 16.10 ................................................................... 2
Introduction .......................................................................................................................... 2
Prerequisites ......................................................................................................................... 2
Step 1: Install Java ................................................................................................................ 2
Step 2: Create Tomcat User ................................................................................................. 3
Step 3: Install Tomcat ........................................................................................................... 3
Step 4: Update Permissions ................................................................................................. 4
Step 5: Create a systemd Service File .................................................................................. 5
Step 6: Adjust the Firewall and Test the Tomcat Server .................................................... 7
Step 7: Configure Tomcat Web Management Interface..................................................... 8
Step 8: Access the Web Interface ........................................................................................ 9
Conclusion ........................................................................................................................... 12
Installing MariaDB on Ubuntu 16.10 ....................................................................................... 13
Installing MariaDB from Ubuntu Repository: ...................................................................... 13
using mysql check the version ............................................................................................. 13
Then create a new user and the passward ......................................................................... 13
Then give the privileges to user ‘bems’ ............................................................................... 13
Then create a database named ‘bems’ ............................................................................... 14
enter to this folder /etc/mysql/mariadb.conf.d............................................................... 14
Editing the defaults file ....................................................................................................... 14
Finally, restart the mariadb ................................................................................................. 15
Last, we can use “mysql workbench” to import the database............................................ 16
Import the database ............................................................................................................ 18
Python client ............................................................................................................................ 19
install python 3.5 ................................................................................................................. 19
WebSockets ......................................................................................................................... 19
PyMySQL .............................................................................................................................. 19
The source code : ................................................................................................................ 20
OpenADR ................................................................................................................................. 24
2 / 28
How To Install Apache Tomcat 8 on Ubuntu
16.10
Introduction
Apache Tomcat is a web server and servlet container that is used to serve
Java applications. Tomcat is an open source implementation of the Java
Servlet and JavaServer Pages technologies, released by the Apache
Software Foundation. This tutorial covers the basic installation and some
configuration of the latest release of Tomcat 8 on your Ubuntu 16.10 server.
Prerequisites Before you begin with this guide, you should have a non-root user
with sudo privileges set up on your server.
Step 1: Install Java Tomcat requires Java to be installed on the server so that any Java web
application code can be executed. We can satisfy that requirement by
installing OpenJDK with apt-get.
First, update your apt-get package index:
• sudo apt-get update
•
Then install the Java Development Kit package with apt-get:
• sudo apt-get install default-jdk
•
Now that Java is installed, we can create a tomcat user, which will be used to
run the Tomcat service.
3 / 28
Step 2: Create Tomcat User For security purposes, Tomcat should be run as an unprivileged user (i.e. not
root). We will create a new user and group that will run the Tomcat service.
First, create a new tomcat group:
• sudo groupadd tomcat
•
Next, create a new tomcat user. We'll make this user a member of
the tomcat group, with a home directory of /opt/tomcat (where we will
install Tomcat), and with a shell of /bin/false (so nobody can log into the
account):
• sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
•
Now that our tomcat user is set up, let's download and install Tomcat.
Step 3: Install Tomcat The best way to install Tomcat 8 is to download the latest binary release then
configure it manually.
Find the latest version of Tomcat 8 at the Tomcat 8 Downloads page. At the
time of writing, the latest version is 8.5.5, but you should use a later stable
version if it is available. Under the Binary Distributions section, then under
the Core list, copy the link to the "tar.gz".
Next, change to the /tmp directory on your server. This is a good directory to
download ephemeral items, like the Tomcat tarball, which we won't need after
extracting the Tomcat contents:
• cd /tmp
•
Use curl to download the link that you copied from the Tomcat website:
4 / 28
• curl -O http://apache.mirrors.ionfish.org/tomcat/tomcat-
8/v8.5.5/bin/apache-tomcat-8.5.5.tar.gz
•
We will install Tomcat to the /opt/tomcat directory. Create the directory,
then extract the archive to it with these commands:
• sudo mkdir /opt/tomcat
•
• sudo tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-
components=1
•
Next, we can set up the proper user permissions for our installation.
Step 4: Update Permissions The tomcat user that we set up needs to have access to the Tomcat
installation. We'll set that up now.
Change to the directory where we unpacked the Tomcat installation:
• cd /opt/tomcat
•
Give the tomcat group ownership over the entire installation directory:
• sudo chgrp -R tomcat /opt/tomcat
•
Next, give the tomcat group read access to the conf directory and all of its
contents, and execute access to the directory itself:
• sudo chmod -R g+r conf
•
• sudo chmod g+x conf
•
Make the tomcat user the owner of the webapps, work, temp,
and logs directories:
• sudo chown -R tomcat webapps/ work/ temp/ logs/
5 / 28
•
Now that the proper permissions are set up, we can create a systemd service
file to manage the Tomcat process.
Step 5: Create a systemd Service File We want to be able to run Tomcat as a service, so we will set up systemd
service file.
Tomcat needs to know where Java is installed. This path is commonly
referred to as "JAVA_HOME". The easiest way to look up that location is by
running this command:
• sudo update-java-alternatives -l
•
Output
java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-
openjdk-amd64
The correct JAVA_HOME variable can be constructed by taking the output from
the last column (highlighted in red) and appending /jre to the end. Given the
example above, the correct JAVA_HOME for this server would be:
JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre
Your JAVA_HOME may be different.
With this piece of information, we can create the systemd service file. Open a
file called tomcat.servicein the /etc/systemd/system directory by
typing:
• sudo nano /etc/systemd/system/tomcat.service
•
6 / 28
Paste the following contents into your service file. Modify the value
of JAVA_HOME if necessary to match the value you found on your system. You
may also want to modify the memory allocation settings that are specified
in CATALINA_OPTS:
/etc/systemd/system/tomcat.service [Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -
XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -
Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
When you are finished, save and close the file.
Next, reload the systemd daemon so that it knows about our service file:
• sudo systemctl daemon-reload
•
Start the Tomcat service by typing:
7 / 28
• sudo systemctl start tomcat
•
Double check that it started without errors by typing:
• sudo systemctl status tomcat
•
Step 6: Adjust the Firewall and Test the Tomcat Server Now that the Tomcat service is started, we can test to make sure the default
page is available.
Before we do that, we need to adjust the firewall to allow our requests to get
to the service. If you followed the prerequisites, you will have a ufw firewall
enabled currently.
Tomcat uses port 8080 to accept conventional requests. Allow traffic to that
port by typing:
• sudo ufw allow 8080
•
With the firewall modified, you can access the default splash page by going to
your domain or IP address followed by :8080 in a web browser:
8 / 28
Open in web browser
http://server_domain_or_IP:8080
You will see the default Tomcat splash page, in addition to other information.
However, if you click the links for the Manager App, for instance, you will be
denied access. We can configure that access next.
If you were able to successfully accessed Tomcat, now is a good time to
enable the service file so that Tomcat automatically starts at boot:
• sudo systemctl enable tomcat
•
Step 7: Configure Tomcat Web Management Interface In order to use the manager web app that comes with Tomcat, we must add a
login to our Tomcat server. We will do this by editing the tomcat-
users.xml file:
• sudo nano /opt/tomcat/conf/tomcat-users.xml
•
You will want to add a user who can access the manager-gui and admin-
gui (web apps that come with Tomcat). You can do so by defining a user,
similar to the example below, between the tomcat-users tags. Be sure to
change the username and password to something secure:
tomcat-users.xml — Admin User
<tomcat-users . . .>
<user username="admin" password="password" roles="manager-
gui,admin-gui"/>
</tomcat-users>
Save and close the file when you are finished.
By default, newer versions of Tomcat restrict access to the Manager and Host
Manager apps to connections coming from the server itself. Since we are
installing on a remote machine, you will probably want to remove or alter this
9 / 28
restriction. To change the IP address restrictions on these, open the
appropriate context.xml files.
For the Manager app, type:
• sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
•
For the Host Manager app, type:
• sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
•
Inside, comment out the IP address restriction to allow connections from
anywhere. Alternatively, if you would like to allow access only to connections
coming from your own IP address, you can add your public IP address to the
list:
context.xml files for Tomcat webapps
<Context antiResourceLocking="false" privileged="true" >
<!--<Valve
className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />-->
</Context>
Save and close the files when you are finished.
To put our changes into effect, restart the Tomcat service:
• sudo systemctl restart tomcat
•
Step 8: Access the Web Interface Now that we have create a user, we can access the web management
interface again in a web browser. Once again, you can get to the correct
interface by entering your server's domain name or IP address followed on
port 8080 in your browser:
Open in web browser
http://localhost:8080
10 / 28
The page you see should be the same one you were given when you tested
earlier:
Let's take a look at the Manager App, accessible via the link
or http://localhost:8080/manager/html. You will need to enter the
account credentials that you added to the tomcat-users.xml file.
Afterwards, you should see a page that looks like this:
11 / 28
The Web Application Manager is used to manage your Java applications. You
can Start, Stop, Reload, Deploy, and Undeploy here. You can also run some
diagnostics on your apps (i.e. find memory leaks). Lastly, information about
your server is available at the very bottom of this page.
Now let's take a look at the Host Manager, accessible via the link
or http://localhost:8080/host-manager/html/:
From the Virtual Host Manager page, you can add virtual hosts to serve your
applications from.
Step 9: copy the file into webserver
open in the web browser again
12 / 28
http://localhost:8080/
Conclusion Your installation of Tomcat is complete! Your are now free to deploy your own
Java web applications!
13 / 28
Installing MariaDB on Ubuntu 16.10
MariaDB is a fork of the very popular MySQL database management system; it is now considered as a full drop-in replacement for MySQL.
Installing MariaDB from Ubuntu Repository:
Installing from Ubuntu default repository is a straightforward way, but this may have bit old version of MariaDB.
sudo apt-get update -y
sudo apt-get install mariadb-server
using mysql check the version
Then create a new user and the passward
CREATE USER 'bems'@'%' IDENTIFIED BY 'nestfield123';
Then give the privileges to user ‘bems’
GRANT ALL ON *.* TO 'bems'@'%';
14 / 28
Then create a database named ‘bems’
So far the database is successfully created. Next Configuring MariaDB for Remote Client Access
enter to this folder /etc/mysql/mariadb.conf.d
vim 50-server.cnf
Editing the defaults file
Once you have located the defaults file, use a text editor to open the file and try to find lines like this under the [mysqld] section:
[mysqld]
...
skip-networking
...
bind-address = <some ip-address>
15 / 28
...
(The lines may not be in this order, and the order doesn't matter.)
If you are able to locate these lines, make sure they are both commented out (prefaced with hash (#) characters), so that they look like this:
[mysqld]
...
#skip-networking
...
#bind-address = <some ip-address>
...
(Again, the order of these lines don't matter)
Finally, restart the mariadb
16 / 28
service mysql restart
Last, we can use “mysql workbench” to import the database
17 / 28
Connection name : test Hostname : your mariadb server IP address (192.168.0.230) Username : bems Password : nestfield123
18 / 28
Import the database
19 / 28
Python client
install python 3.5
sudo apt-get update sudo apt-get install python3
WebSockets
websockets is a library for developing WebSocket servers and clients in Python.
It implements RFC 6455 with a focus on correctness and simplicity. It passes
the Autobahn Testsuite.
Built on top of Python's asynchronous I/O support introduced in PEP 3156, it
provides an API based on coroutines, making it easy to write highly concurrent
applications.
Installation is as simple as pip install websockets. It requires Python ≥ 3.4 or
Python 3.3 with the asyncio module, which is available with pip install
asyncio.
PyMySQL
NOTE: PyMySQL doesn't support low level APIs _mysql provides like
data_seek, store_result, and use_result. You should use high level APIs defined
in PEP 249. But some APIs like autocommit and ping are supported
because PEP 249 doesn't cover their usecase.
20 / 28
Requirements
• Python -- one of the following: o CPython >= 2.6 or >= 3.3
o PyPy >= 4.0
o IronPython 2.7
• MySQL Server -- one of the following: o MySQL >= 4.1 (tested with only 5.5~)
o MariaDB >= 5.1
Installation
The last stable release is available on PyPI and can be installed with pip:
$ pip install PyMySQL
The source code :
#!/usr/bin/env python import asyncio import websockets import http.client import pymysql import time conn = http.client.HTTPConnection("192.168.0.100", 8000) conn.request("GET", "/end2/on") r1 = conn.getresponse() data1 = r1.read() #conn.close() print(data1) conn.request("GET", "/end3/on") r2 = conn.getresponse() data2 = r2.read() #conn.close() print(data2) conn.request("GET", "/end4/on") r3 = conn.getresponse()
21 / 28
data3 = r3.read() #conn.close() print(data3) async def hello(websocket, path): name = await websocket.recv() print("< {}".format(name)) greeting = "Hello {}!".format(name) await websocket.send(greeting) print("> {}".format(greeting)) if (float(name)>=7.0): print(name) conn = http.client.HTTPConnection("192.168.0.100", 8000) conn.request("GET", "/end2/pwm01") r1 = conn.getresponse() data1 = r1.read() #conn.close() print(data1) a = 1 conn.request("GET", "/end3/pwm02") r2 = conn.getresponse() data2 = r2.read() #conn.close() print(data2) b = 2 conn.request("GET", "/end4/pwm03") r3 = conn.getresponse() data3 = r3.read() #conn.close() print(data3) c = 3 elif (float(name)<=3): print(name) conn = http.client.HTTPConnection("192.168.0.100", 8000) conn.request("GET", "/end2/pwm09") r1 = conn.getresponse() data1 = r1.read() #conn.close() print(data1) a = 9 conn.request("GET", "/end3/pwm07") r2 = conn.getresponse()
22 / 28
data2 = r2.read() #conn.close() print(data2) b = 7 conn.request("GET", "/end4/pwm08") r3 = conn.getresponse() data3 = r3.read() #conn.close() print(data3) c = 8 else: conn = http.client.HTTPConnection("192.168.0.100", 8000) conn.request("GET", "/end2/pwm04") r1 = conn.getresponse() data1 = r1.read() #conn.close() print(data1) a = 4 conn.request("GET", "/end3/pwm05") r2 = conn.getresponse() data2 = r2.read() #conn.close() print(data2) b = 5 conn.request("GET", "/end4/pwm06") r3 = conn.getresponse() data3 = r3.read() #conn.close() print(data3) print(name) c = 6 #count = 0 # Connect to the database db = pymysql.connect(host='localhost', user='bems', password='nestfield123', db='bems', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) count = 0 cursor = db.cursor() timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
23 / 28
sql = """INSERT INTO reading(href_R, timePeriod, value, localID, price) VALUES (%s, %s, %s, %s, %s)""" energys = [('edev1', timestamp, data1, '01', name), ('edev2',timestamp, data2, '02', name), ('edev3',timestamp, data3, '03', name)] sqlcontrol = """INSERT INTO enddevicecontrol(href_ED, mRID, description, version, deviceCategory, creationTime, state, value) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""" controls = [('edev1', '01', '6LoWPAN1', '1.0', '09', timestamp, '1', '4'), ('edev2', '02', '6LoWPAN2', '1.0', '09', timestamp, '1', '5'), ('edev3', '03', '6LoWPAN3', '1.0', '09', timestamp, '1', '9')] try: cursor.executemany(sql,energys) #cursor.executemany(sqlcontrol,controls) #cursor.exectuemany("UPDATE enddevicecontrol SET value = %s WHERE href_ED = %s", [('1', 'edev1'),('1', 'edev2'),('1', 'edev3')]) db.commit() except: db.rollback() count = count + 1 #time.sleep(10) print ("the count is :", count) print ("the time is :", timestamp) db.close() start_server = websockets.serve(hello, 'localhost', 6000) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever()
24 / 28
OpenADR
System Requirements EPRI’s OADR VTN was developed and tested on an Ubuntu 14.04/16.04 desktop and server. Limited testing has been done on Mac OS X. The software has not been tested on a Windows server, although all of the software used to run the OADR VTN runs on Windows. The target system should have at least 4GB of ram and 20GB of free hard disk space. Installation Notes The following instructions are for Ubuntu Server 14.04. Configuring Torquebox, MySQL, and Apache is a complex process. The following instructions are meant as a guideline for running the OADR VTN, not a template for securing a production environment. It is advisable not to expose the OADR VTN outside your firewall if SSL is not turned on because the OpenADR services (EiEvent, EiReport, EiOpt, and EiRegisterParty) do not perform authentication. The services assume that SSL authentication using client-side certificates has been performed before messages reach them. The OADR VTN is designed to be deployed behind an Apache reverse proxy server, which handles SSL. A sample Apache config file is included in the documentation directory for the server. The following instructions can be adapted to most flavors of Linux, though they were tested on Ubuntu. Set up and Configure the Server 1. Install Java 7: sudo apt-get install openjdk-7-jdk This will install the OpenJDK version of Java, not the Sun/Oracle version. Some features may not be available in the OpenJDK version. If you experience issues, please follow your operating system instructions for installing Java from Oracle. This version of Java does not exist by default on Ubuntu 16.04. When installing on Ubuntu 16.04, run the following before running the install command from above: sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-get update 2. Install a database: a. The default database for the application is PostgreSQL. Ubuntu installation instructions can be found here: https://help.ubuntu.com/community/PostgreSQL.
25 / 28
Any database that supports Any Active Record can be used, but the application is written and tested with PostgreSQL.
b. Create a database for the application.
c. Create a user for the application and give the user full rights to the database created in the previous step. 3. Setup a torquebox user and install torquebox: sudo adduser torquebox –disabled-login sudo mkdir /opt/torquebox sudo chown torquebox:torquebox /opt/torquebox wget http://repository-projectodd.forge.cloudbees.com/release/org/torquebox/torquebox-dist/3.0.2/torquebox-dist-3.0.2-bin.zip sudo su torquebox unzip torquebox-dist-3.0.2-bin.zip –d /opt/torquebox/ cd /opt/torquebox ln –s torquebox-3.0.2 current 4. Add the following lines to the torquebox user’s ~/.bashrc file: export TORQUEBOX_HOME=/opt/torquebox/current export JBOSS_HOME=$TORQUEBOX_HOME/jboss export JRUBY_HOME=$TORQUEBOX_HOME/jruby PATH=$JBOSS_HOME/bin:$JRUBY_HOME/bin:$PATH export RAILS_ENV=production
5. To make the changes in step 4 available to the current terminal, run the following command: source ~/.bashrc Alternatively, restart the console. These paths are needed for the next section. Set Up the Application The following commands should all be run as the torquebox user. To switch to the torquebox user, run sudo su torquebox 1. Copy oadr-0.9.7.zip to /home/torquebox/ and unzip the file and move the directory to oadr: unzip oadr-0.9.7.zip
26 / 28
mv oadr-0.9.7 oadr After extracting the archive, the tar.gz file can be removed. 2. Switch to /home/torquebox/oadr. If you receive errors running the following commands, double-check that the export paths above (steps 4 and 5) are set correctly and torquebox is installed correctly.
3. Install rails: gem install rails -v 3.2.12 4. Install the application gems: bundle install 5. Prepare the application assets (this command will take a few minutes: be patient): rake assets:precompile 6. Configure the application database: a. Copy config/database.yml.example to config/database.yml
b. Edit the production section of config/database.yml to set the appropriate database, username, password, and host. 7. Configure torquebox services: Copy config/torquebox.yml.example to config/torquebox.yml The default settings will work for most applications. If you require XMPP or need to enable or disable host name validation on push VENs, this file will need to be modified. Modifying the torquebox config file requires that the service be restarted before changes will take effect. 8. Set up a secret token: a. Copy config/initializers/secret_token.rb.example to config/initializers/secret_token.rb
b. Generate a secret token: rake secret c. Copy the output from the above command over the “place token here” text in the file config/initializers/secret_token.rb 9. Set up the database: rake db:setup 10. Seed the database: rake db:seed 11. Load test case prompts (if desired):
27 / 28
rake db:loadtests 12. Deploy the application to production: torquebox deploy 13. Test the installation by running torquebox: torquebox run –b 0.0.0.0 This will start torquebox listening on all local interfaces on port 8080, allowing you to test that the application is working. Load the login screen in a browser by visiting http://localhost:8080. After verifying that the login screen loads, hit <ctrl+c> to stop torquebox. 14. Next, install the torquebox upstart script. The oadr/documentation directory contains a script called torquebox.conf and copy this file (as root) to /etc/init/
15. Start torquebox through upstart: sudo start torquebox Similarly, to stop torquebox, run sudo stop torquebox Note that the upstart script will start torquebox when the server restarts. 16. Verify that torquebox is running by tailing the log file: tail –f /var/log/torquebox/torquebox.log 17. By default, torquebox will listen on 127.0.0.1. To make the application available outside the localhost, a reverse proxy should be configured to redirect requests to localhost port 8080. Alternatively, though this isn’t recommended, change the inet-address parameter in /opt/torquebox/current/jboss/standalong/configuration/standalone.xml
18. After starting torquebox, check that the application started without issue by looking at the torquebox log file in /var/log/torquebox/torquebox.log for errors like this: (NameError) missing class or uppercase package name (‘epri.oadr2b.lib.OadrPayload’)
This error indicates that torquebox is finding the wrong JVM. The application will not run under Java 6 and you may need to uninstall Java 6. On Ubuntu, run the following command to make Java 7 the default: sudo update-alternatives –config java After running the above command, you will be presented with a list of options. Type the number that corresponds to Java 7 and press Enter.
28 / 28
Log Files Should you run into issues, the following log files should be examined: Application: /home/torquebox/oadr/log/[production | error | info].log.
Apache: /var/log/apache2/*
Torquebox: /var/log/torquebox/torquebox.log The log files in /home/torquebox/oadr/log can grow very large. It’s advisable to manage the files with logrotate. A sample logrotate file is include under documentation/logrotate. On Ubuntu 14.04, this file can be copied to /etc/logrotate.d/ to enable.