Monthly Archives: 1月 2017

CentOSにnginx起動スクリプトのサンプルコード

nginxコード:
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -f /etc/default/nginx ]; then
. /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
test_nginx_config() {
if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then
return 0
else
$DAEMON -t $DAEMON_OPTS
return $?
fi
}

case “$1” in
start)
echo -n “Starting $DESC: ”
test_nginx_config
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n “$ULIMIT” ]; then
# Set the ulimits
ulimit $ULIMIT
fi
start-stop-daemon –start –quiet –pidfile /usr/local/nginx/logs/$NAME.pid \
–exec $DAEMON — $DAEMON_OPTS || true
echo “$NAME.”
;;

stop)
echo -n “Stopping $DESC: ”
start-stop-daemon –stop –quiet –pidfile /usr/local/nginx/logs/$NAME.pid \
–exec $DAEMON || true
echo “$NAME.”
;;

restart|force-reload)
echo -n “Restarting $DESC: ”
start-stop-daemon –stop –quiet –pidfile \
/usr/local/nginx/logs/$NAME.pid –exec $DAEMON || true
sleep 1
test_nginx_config
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n “$ULIMIT” ]; then
# Set the ulimits
ulimit $ULIMIT
fi
start-stop-daemon –start –quiet –pidfile \
/usr/local/nginx/logs/$NAME.pid –exec $DAEMON — $DAEMON_OPTS || true
echo “$NAME.”
;;

reload)
echo -n “Reloading $DESC configuration: ”
test_nginx_config
start-stop-daemon –stop –signal HUP –quiet –pidfile /usr/local/nginx/logs/$NAME.pid \
–exec $DAEMON || true
echo “$NAME.”
;;
configtest|testconfig)
echo -n “Testing $DESC configuration: ”
if test_nginx_config; then
echo “$NAME.”
else
exit $?
fi
;;

status)
status_of_proc -p /usr/local/nginx/logs/$NAME.pid “$DAEMON” nginx && exit 0 || exit $?
;;
*)
echo “Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}” >&2
exit 1
;;
esac

exit 0

UbuntuでPHPをコンパイルする

1.依存パッケージのインストール
#sudo apt-get install libjpeg-dev
#sudo apt-get install libxml2-dev
#sudo apt-get -y install libjpeg-dev
#sudo apt-get install libmcrypt-dev
#sudo apt-get install libpng12-dev
#sudo apt-get -y install libfreetype6-dev
#sudo apt-get install curl libcurl3 libcurl3-dev

2.コンパイル
#sudo ./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php –enable-fpm –with-fpm-user=php-fpm –with-fpm-group=php-fpm –with-mysql=mysqlnd –with-pdo-mysql=mysqlnd –with-mysqli=mysqlnd –with-libxml-dir –with-gd –with-jpeg-dir –with-png-dir –with-freetype-dir –with-iconv-dir –with-zlib-dir –with-mcrypt –enable-soap –enable-gd-native-ttf –enable-ftp –enable-mbstring –enable-exif –disable-ipv6 –with-pear –with-curl –enable-bcmath –enable-mbstring –enable-sockets –with-gd –with-libxml-dir=/usr/local –with-gettext

3.php.iniの修正
#sudo cp php.ini-development /usr/local/php/php.ini
#sudo vim /usr/local/php/php.ini
data.timezone = PRC

4.php-fpmの修正
#cd /usr/local/php/etc
#sudo cp php-fpm.conf.default php-fpm.conf
#vim php-fpm.conf
#pidとエラーログの有効
pid = run/php-fpm.pid
error_log = log/php-fpm.log
user = ttuser
group = ttuser

5.php起動コマンド
#sudo cp -r /usr/local/src/php-5.6.26/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#sudo mkdir -p /usr/local/php/etc/fpm.d
#sudo chmod +x /etc/init.d/php-fpm

6.php-fpmの起動
#sudo /etc/init.d/php-fpm (start|stop|force-quit|restart|reload|status)

php-fpm自動起動
#sudo apt-get install sysv-rc-conf
#sudo sysv-rc-conf php-fpm on

Ubuntu で MySQLをインストールする

1.圧縮ファイルを解凍
#sudo tar zxvf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz
#sudo mv mysql-5.6.33-linux-glibc2.5-x86_64 /usr/local/mysql

2.mysqlグループとユーザの作成
#groupadd mysql
#useradd -g mysql mysql -s /bin/false
#sudo chown -R mysql:mysql /usr/local/mysql

3.mysqlの初期化
#sudo rm -rf /etc/my.cnf
#sudo ./scripts/mysql_install_db –user=mysql –basedir=/usr/local/mysql

4.mysqlの権限を設定
#sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
#sudo chmod +x /etc/init.d/mysql

5.mysql起動
#sudo /etc/init.d/mysql (start|stop|force-quit|restart|reload|status)

6.mysqlのパスワードを設定
#cd /usr/local/mysql/bin
#sudo ./mysqladmin -u root password root(パスワード)

7.mysql clientでDBに接続
#cd /usr/local/mysql/bin
#sudo ./mysql -u root -p

8.mysqlステータス
#sudo lsof -i:3306

9.自動起動
#sudo apt-get install sysv-rc-conf
#sudo sysv-rc-conf php-fpm on

10.外部ホストアクセスの許可
#grant all privileges on *.* to startuser@”%” identified by ‘pwdstartnews24’ with grant option;
#flush privileges;

java開発–SimpleTagインタフェースによるカスタムタグを定義する

Javaコード:
public class SimpleTagSupport implements SimpleTag
{
private JspTag parentTag;
private JspContext jspContext;

private JspFragment jspBody;

public SimpleTagSupport() { }

public void doTag() throws JspException, IOException
{
}

public void setParent( JspTag parent ) {
this.parentTag = parent;
}

public JspTag getParent() {
return this.parentTag;
}

public void setJspContext( JspContext pc ) {
this.jspContext = pc;
}

protected JspContext getJspContext() {
return this.jspContext;
}

public void setJspBody( JspFragment jspBody ) {
this.jspBody = jspBody;
}

protected JspFragment getJspBody() {
return this.jspBody;
}

public static final JspTag findAncestorWithClass(
JspTag from, Class<?> klass)
{
boolean isInterface = false;

if (from == null || klass == null
|| (!JspTag.class.isAssignableFrom(klass)
&& !(isInterface = klass.isInterface()))) {
return null;
}

for (;;) {
JspTag parent = null;
if( from instanceof SimpleTag ) {
parent = ((SimpleTag)from).getParent();
}
else if( from instanceof Tag ) {
parent = ((Tag)from).getParent();
}
if (parent == null) {
return null;
}

if (parent instanceof TagAdapter) {
parent = ((TagAdapter) parent).getAdaptee();
}
if ((isInterface && klass.isInstance(parent))
|| klass.isAssignableFrom(parent.getClass())) {
return parent;
}

from = parent;
}
}
}

Java学習–二分木(binary tree)クラスの定義と使い方

1.二分木クラスの定義
package org.tang.binarytree;

public class BinaryTree<T extends Comparable<T>> {
private Node<T> root;

public Node<T> getRoot() {
return root;
}

public void setRoot(Node<T> root) {
this.root = root;
}

public void addElement(T element){
if(element == null){
return;
}
Node<T> node = new Node<T>(element);
if(this.root == null){
this.root = node;
}else{
this.root.addChild(node);
}
}

public void printBinaryTree(){
if(this.root == null){
System.out.println(“can not print null object”);
return;
}
this.root.printNode();
}

private static class Node<E extends Comparable<E>>{
private E nodeData;
private Node<E> left;
private Node<E> right;
private Node(E nodeData){
this.nodeData = nodeData;
}

private void addChild(Node<E> child){
if(child == null){
return;
}
if(this.compareTo(child) >= 0){
if(this.left == null){
this.left = child;
}else{
this.left.addChild(child);
}
}else{
if(this.right == null){
this.right = child;
}else{
this.right.addChild(child);
}
}
}

private int compareTo(Node<E> node){
return this.nodeData.compareTo(node.nodeData);
}

private void printNode(){
if(this.left != null){
this.left.printNode();
}
System.out.println(this.nodeData.toString());
if(this.right != null){
this.right.printNode();
}
}
}
}
2.Mainプログラム
package org.tang.binarytree;
public class Client {
public static void main(String[] args){
BinaryTree<Integer> qtm = new BinaryTree<Integer>();
qtm.addElement(98);
qtm.addElement(42);
qtm.addElement(36);
qtm.addElement(1);
qtm.addElement(24);
qtm.addElement(-1);
qtm.printBinaryTree();

BinaryTree<String> yc2 = new BinaryTree<String>();
yc2.addElement(“b”);
yc2.addElement(“B”);
yc2.addElement(“y”);
yc2.addElement(“f”);
yc2.printBinaryTree();
}
}

Java入門–配列のdeepcopyを実現する方法

1.JavaBeanクラス
class Employee implements Serializable{
private int age;
private String name;

public Employee(){};
public Employee(int age,String name){
this.age=age;
this.name=name;
}

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String toString(){
return this.name+”–>”+this.age;
}

}
2.Listを出力
public static <T> void printList(List<T> list){
System.out.println(“—begin print list—“);
for(T t : list){
System.out.println(t);
}
System.out.println(“—end print list—“);
}

3.配列を出力
public static <T> void printArray(T[] array){
System.out.println(“—begin print array —“);
for(T t : array){
System.out.println(t);
}
System.out.println(“—end print array—“);
}
4.deepCopyの実現方法
public static <T> List<T> deepCopyFunc(List<T> src) throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);

ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings(“unchecked”)
List<T> dest = (List<T>) in.readObject();
return dest;
}
List<Employee> destList=deepCopyFunc(srcList);
printList(destList);
srcList.get(0).setAge(100);
printList(destList);

Struts2入門–RequestAwareとSessionAwareを使ってRequestオブジェクトを取得する

Javaコード:
import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class Logincft2Action extends ActionSupport
implements RequestAware , SessionAware, ApplicationAware{
private Map request;
private Map session;
private Map application;

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.request.put(“attribute”, “request value servlet not IoC “);
this.session.put(“attribute”, “session value servlet not IoC “);
this.application.put(“attribute”, “application value servlet not IoC “);
return SUCCESS;
}

@Override
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.request = arg0;
}

@Override
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.session = arg0;
}

@Override
public void setApplication(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.application = arg0;
}
}

Struts2入門–ActionSupportでRequest、Sessionを取得する

Javaコード:
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Logincft3Action extends ActionSupport{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.request = ServletActionContext.getRequest();
this.session = this.request.getSession();
this.application = ServletActionContext.getServletContext();
this.request.setAttribute(“attribute”, “request value servlet not IoC “);
this.session.setAttribute(“attribute”, “session value servlet not IoC “);
this.application.setAttribute(“attribute”, “application value servlet not IoC “);
return SUCCESS;
}

}

Struts2入門–ServletRequestAwareでrequest、sessionオブジェクトを取得する

Actionコード:
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class Logincft4Action extends ActionSupport
implements ServletRequestAware , ServletContextAware{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
this.request.setAttribute(“attribute”, “request value servlet IoC “);
this.session = this.request.getSession();
this.session.setAttribute(“attribute”, “session value servlet IoC “);
this.application.setAttribute(“attribute”, “application value servlet IoC “);
return SUCCESS;
}

@Override
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
this.request = arg0;
}

@Override
public void setServletContext(ServletContext arg0) {
// TODO Auto-generated method stub
this.application = arg0;
}
}

Java入門–ファイルのMD5を求める方法

Javaコード:
package com.cftpj;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
protected static char hexDigits[] = { ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ };
protected static MessageDigest messagedigest = null;
static {
try {
messagedigest = MessageDigest.getInstance(“MD5”);
} catch (NoSuchAlgorithmException nsaex) {
System.err.println(MD5Util.class.getName()
+ “MessageDigestはMD5Utilをサポートしない”);
nsaex.printStackTrace();
}
}
public static String getFileMD5String(File file) throws IOException {
FileInputStream in = new FileInputStream(file);
FileChannel ch = in.getChannel();
MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,
file.length());
messagedigest.update(byteBuffer);
return bufferToHex(messagedigest.digest());
}
public static String getMD5String(String s) {
return getMD5String(s.getBytes());
}

public static String getMD5String(byte[] bytes) {
messagedigest.update(bytes);
return bufferToHex(messagedigest.digest());
}

private static String bufferToHex(byte bytes[]) {
return bufferToHex(bytes, 0, bytes.length);
}

private static String bufferToHex(byte bytes[], int m, int n) {
StringBuffer stringbuffer = new StringBuffer(2 * n);
int k = m + n;
for (int l = m; l < k; l++) {
appendHexPair(bytes[l], stringbuffer);
}
return stringbuffer.toString();
}

private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
char c0 = hexDigits[(bt & 0xf0) >> 4];
char c1 = hexDigits[bt & 0xf];
stringbuffer.append(c0);
stringbuffer.append(c1);
}
public static boolean checkPassword(String password, String md5PwdStr) {
String s = getMD5String(password);
return s.equals(md5PwdStr);
}

public static void main(String[] args) throws IOException {
long begin = System.currentTimeMillis();

File big = new File(“D:/Users/Kit/Desktop/demo11.txt”);
String md5 = getFileMD5String(big);
long end = System.currentTimeMillis();
System.out.println(“md5:” + md5 + “,time:” + ((end – begin) / 1000) + “s”);
long begin2 = System.currentTimeMillis();
File big2 = new File(“D:/Users/Kit/Desktop/demo12.txt”);
String md52 = getFileMD5String(big2);
long end2 = System.currentTimeMillis();
System.out.println(“md5:” + md52 + “,time:” + ((end2 – begin2) / 1000) + “s”);
FileInputStream is = new FileInputStream(“”);
}
}

1 / 1212345...10...最後 »